Functions vs Methods
In c sharp any reusable block of code is a function. A function can also be a method if it is part of a class. When you simply declare a "method" in your main file, it is actually just a local function.
Defining and calling a method
CountTo(5);
void CountTo(int max)
{
for (int i = 0; i < max; i++)
{
Console.Write($"{i}, ");
}
}
When an argument is passed to a method, value type variables have their values copied into the method. Each variable has its own copy of the value, so the original variable isn't modified.
With reference types, the address of the value is passed into the method. The variable given to the method references the value at that address, so operations on that variable affect the value that is referenced by the other.
It is important to remember that string is a reference type, but it is immutable. That means once it has been assigned a value, it can't be altered. In C#, when methods and operators are used to modify a string, the result that is returned is actually a new string object.
Named Parameters
void RSVP(string name, int partySize, string allergies, bool inviteOnly)
{
...
}
RSVP(name: "Linh", partySize: 2, allergies: "none", inviteOnly: false);
Optional Parameters
A parameter becomes optional when it's assigned a default value. In this example only name is required:
void RSVP(string name, int partySize = 1, string allergies = "none", bool inviteOnly = true) { ... }
Overloaded methods
Many methods in .NET have overloaded method signatures. Among other things, this enables you to call the method with or without arguments:
int number = 7;
string text = "seven";
Console.WriteLine(number);
Console.WriteLine();
Console.WriteLine(text);
When defining your own methods you simply declare the same name, but with different parameters.
Arrow notation
Sometimes a method is so trivial it's less noisy to write it inline:
int DoubleAndAddOne(int value)
{
return value * 2 + 1;
}
// or
int DoubleAndAddOne(int value) => value * 2 + 1;
Stateful vs Stateless
Random dice = new Random();
int roll = dice.Next(1, 7);
Console.WriteLine(roll);
Some methods, such as Console.WriteLine() don't rely on the current state of
the program. These are known as stateless methods. Other methods, however, must
have access to the state of the application to work or they modify the state of
the application by updating values and storing new values in memory. These are
known as Stateful or Instance methods.
Determining if a class is stateless or stateful
The documentation for a class will show examples of how it is used which can often be used to determine this. Also trying to call a stateful method statelessly will produce an error like the following:
int result = Random.Next();
(1,14): error CS0120: An object reference is required for the non-static field, method, or property 'Random.Next()'
Creating an instance of a class
An instance of a class is called an object. To create an instance of a class use
the new operator:
Random dice = new Random();
The new operator does a few important things:
- It first requests an address in memory large enough to store the new object.
- It creates the new object and stores it at that memory address.
- It returns the memory address, which can be stored in a variable.
That variable then points to the state of your object.
The newest versions of .NET allow creating classes with this syntax instead:
Random dice = new();