This content originally appeared on DEV Community and was authored by Eduardo Julião
As we write code to our software, it can get messy and complicated, to solve this issue, we break small parts of our software into methods
.
A method is a block of instructions that are executed only when it's called, you can pass parameters to it and have it return a value.
Creating a method
A method has the follwing structure:
<access modifier> <instance or static> <return type> <method name>(<paramenters>)
{
}
Examples
This an exmaple of a method that can be accessed by everyone, indicated by the public
access modifier.
Does not have a return, indicate by the void
keyword.
Has a name of WriteToConsole
.
And recieves one parameter, indicated by string message
public void WriteToConsole(string message)
{
// code
}
For the method Add
, it recieves to paramenters value1
and value2
, both of type int
, and returns the sum of both paramenters, indicated by the return
keyword.
public int Add(int value1, int value2)
{
return value1 + value2;
}
Breaking down these examples
Access Modifier
Access Modifier
defines how the code can access part of your code. They are used not only for methods, but for classes, properties, enums, etc.
In C# there are four types of access modifiers:
Modifier | Description |
---|---|
private | The code can only be accessed inside the same class. |
public | The code can be accessed for everyone. |
internal | The code can only be accessed. |
protected | The code can only be accessed inside the same class or classes that inherit from the declaring class. |
You can also combine them, for example:
// Accessed public inside the project
public internal ...
// Accessed inly by classes that inherit an inside the same project
protected internal
Static vs Instance
A method that are not defined as static by the static
keyword, is always an instance method.
Instance
To access an Instance
method, first, you need to instantiate an object and it works with the instance data.
Let's use our calculator class as an example:
public class Calculator
{
public int Add(int x, int y)
{
return x + y;
}
}
public static Main()
{
Calculator calc = new Calculator();
int result = calc.Add(5,6);
}
Now, to exemplify what instance data are, let's modify our calculator class, our new calculator class, will have the properties X and Y, and a parameterless method.
public class Calculator
{
public int X {get; set;}
public int Y {get;set;}
public int Add()
{
return X + Y;
}
}
To use this:
static void Main()
{
Calculator calc1 = new Calculator();
calc1.X = 5;
calc1.Y = 10;
int result1 = calc1.Add();
Calculator calc2 = new Calculator();
calc2.X = 22;
calc2.Y = 20;
int result2 = calc2.Add();
Console.WriteLine(result1); // Prints 15
Console.WriteLine(result2); // Prints 42
}
Static
A static method is a unique to the application, meaning that everyone will use the same copy of the method. These methods lives in the Type level instead of a Instance level.
public class Calculator
{
public static int Add(int x, int y)
{
return x + y;
}
}
public static void Main()
{
// calling a static method.
int result = Calculator.Add(5,6);
}
An instance class can have multiple static/instance methods.
A static class can only have static methods and members.
A static method cannot be called from an instance of the object.
Return Types
A method can return a value to its caller, the type returned by the method are indicated before the method name, and they must have the return
keyword. void
methods does not return a value.
public void ExecuteAndDoesNotReturnAValue()
{
// code
{
public string ExecuteAndReturnsAValue()
{
return "This method has a return value";
}
Parameters and Arguments
Parameters are the options listed in the method signature. Arguments are the values passed to the method.
// The method signatures has parameters
// In this case, two paramenters, x and y
public int Add(int x, int y)
{
return x + y;
}
public static Main()
{
Calculator calc = new Calculator();
// When calling the method, we pass **arguments** to its **paramenters**
// in this scenario, we are passing 5 to x and 6 to y
int result = calc.Add(5,6);
}
Polymorphism
Polymorphism is when the same method has multiple functionalities.
public class Calculator
{
/// returns the sum of a and b
public int Add(int a, int b)
{
return a + b;
}
// returns the sum of a, b and c
public int Add(int a, int b, int c)
{
return a + b + c;
}
}
In the example the calculator class have two methods to add values, one that recieves 2 arguments, and another one that recieves 3 arguments.
This content originally appeared on DEV Community and was authored by Eduardo Julião
Eduardo Julião | Sciencx (2021-07-12T14:06:50+00:00) Methods. Retrieved from https://www.scien.cx/2021/07/12/methods/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.