C# methods

C# methods

Methods are basically the places you put code so the program executes it. They are generally within a class and the main method of a class is where the program starts executing statements when running that code(unless another class is calling a method from that class). Generally the main method will call a constructor(link to a different post eventually) to make a object of that class, and/or call a different method to perform some sort of action.

The format for making a method is

accessmodifier optionalmodifier returntype MethodName( parametertype1 parameterName1, parametertype2, parameterName2)

{

//statements

}

The default access modifier is private, and putting it there is optional, so long as you want the access level to be private, if you want it to be public you have to put public. The optional modifiers i am talking about are things like abstract, sealed, static, ect which modify how you can use the the method. The return type is the datatype, or reference type you want your method to return to the place it was called from. If there isn’t a return type the keyword void should be in its place. The parameters are variables you are giving the method when you call it, if the method isn’t suppose to take any variables, leave the parenthesis empty.

When calling a method you have to pay attention to the return type if its a void method you need to call it something like:

object.MethodName( parameterName1 , parameterName2);

if it has a returntype it will need to be something like:

returntype variuableName = obj.MethodName(parameterName1, parameterName2);

The difference between the two is essentially that void is just performing a operation and shouldn’t be expected by the code to return something, while with a returntype the method does need somewhere to put the value it is returning.

The main method should be formatted along the lines of:

accessmodifier static void Main()

{

//statements

}

That is all the information I consider to be basic information there is of course more information at the Microsoft doc for c# methods.