A C# method is a term used to describe functions exposed by objects. A method is a function member exposed by a class. Methods are used to provide access to the object’s functionality. You can also pass data into a method and receive data back as output.
A method has two major sections: a method header and a method body. The method header specifies whether the method returns data and, if so, what type, the name of the method and what types of data can be passed to and from the method and how that data should be treated.
The method body contains the sequence of executable code statements. Execution starts at the first statement in the method body and continues sequentially through the method. The method body is a block. A block is a sequence of statements between curly braces.
Just like fields and properties they can be public or private. Public and private are examples of access modifiers. Methods often make use of an object’s state to affect their operations and have access to private members such as private fields. Public methods in C# are, by convention, named using PascalCasing.
There are a few topics to cover under methods. First we start with signatures. Then we discuss overloading. Next is the params modifier, ref and out modifier, defensive programming and finally we discuss parse and tryparse.
Signature
The signature of a method is the name, the number of parameters and the type of the parameters. Those items create a signature. If all three are the same, then the signature is the same. If at least one of those things are different, the signature is different. Imagine we have a graphics program where we need to move to a different location.
Below is an example of a class that only has one method. The Move, int and int are the method’s signature.
public class Point { public void Move(int x, int y) {} }
What if our method returned an int? Would that constitute a different signature?
Code Inside the Method Body
What can you do inside a method body? There are several things you can do. The code at the bottom of this post illustrates number one and three of the list below.
- Local variables
- Flow-of-control constructs (selection, iteration and jump)
- Method invocations (call other methods from inside a method body)
- Blocks nested within it
- Other methods, known as local functions
Local Variables
What is the difference between local variables in a method or instance fields? This is an important question. Local variables are used inside a method and are usually created to store data for local, or transitory, computations. Let’s start with the syntax of a local variable in a method. The following line of code shows the syntax of local variable declarations. The optional initializer consists of an equal sign followed by a value to be used to initialize the variable
int myint = 5; // type identifier = value;
The existence and lifetime of a local variable is limited to the block in which it is created and the blocks nested within that block. The variable comes into existence at the point at which it is declared. It goes out of existence when the block completes execution. You can declare local variables at any position in the method body, but you must declare them before you can use them.
A local variable can be of type class that you have created. For example, suppose you have a class called PersonClass. You can declare that inside your method. Below is some code I wrote to illustrate.
using System; namespace ConsoleApp2 { class PersonClass { public string FirstName { get; set; } } class MyClass { public int FieldInt = 7; // field public void MyMethod() // method { int localInt = 5; // type identifier = value PersonClass pc = new PersonClass(); // local variable pc pc.FirstName = "Joe"; Console.WriteLine("My local int is: " + localInt.ToString()); Console.WriteLine(pc.FirstName); } // the local variables localInt and pc are now out of scope and un-usable } class Program { static void Main(string[] args) { MyClass mc = new MyClass(); // instantiate mc.MyMethod(); // call MyMethod which uses a local variable Console.WriteLine("Field is: " + mc.FieldInt.ToString()); } } }