Methods and Parameters
Part of the Object Oriented Programming section of Coddy's C# journey — lesson 5 of 70.
Methods define actions that an object can perform. Parameters allow you to pass data into methods, and return types specify what data comes back.
Define a method with parameters and a return type
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}Method with multiple parameters
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public double Multiply(double x, double y)
{
return x * y;
}
}Method with no return value (void)
public class Printer
{
public void PrintMessage(string message)
{
Console.WriteLine(message);
}
}Calling methods with arguments
Calculator calc = new Calculator();
int result = calc.Add(5, 3);
Console.WriteLine(result); // Output: 8
double product = calc.Multiply(2.5, 4.0);
Console.WriteLine(product); // Output: 10Method with string parameter and return
public class Greeter
{
public string CreateGreeting(string name)
{
return $"Hello, {name}!";
}
}
Greeter greeter = new Greeter();
string message = greeter.CreateGreeting("Alice");
Console.WriteLine(message); // Output: Hello, Alice!Parameters are variables that receive values when the method is called. The return type (like int, string, or void) specifies what kind of data the method gives back. Use void when a method doesn't return anything.
Challenge
MediumCreate three methods in the Calculator class:
Add- takes two integers and returns their sumSubtract- takes two integers and returns their difference (first - second)Multiply- takes two integers and returns their product
Cheat sheet
Methods define actions that an object can perform. Parameters allow you to pass data into methods, and return types specify what data comes back.
Define a method with parameters and a return type:
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}Method with no return value (void):
public class Printer
{
public void PrintMessage(string message)
{
Console.WriteLine(message);
}
}Calling methods with arguments:
Calculator calc = new Calculator();
int result = calc.Add(5, 3);
Console.WriteLine(result); // Output: 8Method with string parameter and return:
public class Greeter
{
public string CreateGreeting(string name)
{
return $"Hello, {name}!";
}
}Parameters are variables that receive values when the method is called. The return type (like int, string, or void) specifies what kind of data the method gives back. Use void when a method doesn't return anything.
Try it yourself
using System;
class Program
{
static void Main()
{
int num1 = int.Parse(Console.ReadLine());
int num2 = int.Parse(Console.ReadLine());
Calculator calc = new Calculator();
Console.WriteLine($"Add: {calc.Add(num1, num2)}");
Console.WriteLine($"Subtract: {calc.Subtract(num1, num2)}");
Console.WriteLine($"Multiply: {calc.Multiply(num1, num2)}");
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesNamespaces & DirectivesIntro to Classes & ObjectsThe 'this' KeywordMethods and ParametersFields vs PropertiesConstructorsObject InitializersRecap - Simple Calculator4Inheritance
Basic Inheritance (:) SyntaxThe 'base' KeywordVirtual & Override KeywordsSealed ClassesThe 'object' Base ClassRecap - Employee Hierarchy7Advanced Features
Operator OverloadingIndexers (this[])ToString() OverrideExtension MethodsRecap - Custom List2Properties & Static Members
Auto-Implemented PropertiesRead/Write-Only PropertiesStatic Fields & MethodsStatic ClassesExpression-Bodied Members5Polymorphism & Interfaces
Compile vs Runtime PolyInterface vs Abstract ClassMultiple InterfacesExplicit InterfacesUpcasting & DowncastingRecap - Shape Calculator