In c#, the Method is a separate code block, and that contains a series of statements to perform particular operations. Methods must be declared either in class or struct by specifying the required parameters.
Generally, methods are useful to improve code reusability by reducing code duplication. If we have the same functionality to perform in multiple places, then we can create one method with the required functionality and use it wherever it is required in the application.
As discussed, c# Methods must be declared either in a class or struct by specifying the required access level, return type, name of the method, and any method parameters as shown below.
If you observe the above syntax, we defined the method in a class with various parameters, which are
Following are examples of defining the different types of methods in the c# programming language.
If you observe the above example, we defined different methods with different access modifiers, return types, and different parameters based on our requirements.
Now we will see the complete example using the methods in the c# programming language.
Following is an example of using methods in the c# programming language.
If you observe the above example, we created a GetUserDetails method and passed parameters to perform the required operations. We are accessing the GetUserDetails method by creating an instance of the Program class in the Main method to show the result.
When we execute the above c# program, we will get the result below.
This is how we can use methods in c# applications based on our requirements.
In c#, if we create methods with static, then we can directly invoke those methods from the class level without creating an object.
If you observe the above example, the Main method is static, and we are invoking the GetUserDetails method by creating an instance of the Program class. Instead, if we create a static method, we can directly access those methods in the Main() method without creating an instance of an object.
Following is the example of creating static methods to access those methods directly in the Main() method without creating an instance of an object in c#.
If you observe the above example, we created a method GetUserDetails() with static. Hence, we can access that method directly in the Main() method without creating an instance of a class object.
When we execute the above c# program, we will get the result below.
This is how we can create static methods in c# applications to access without creating an instance of an object based on our requirements.
As per our requirements, we can create methods in c# applications with or without return types and parameters. If we use void as a return type for the method, then that method won’t return any value.
Following is the example of creating a method without having any return value and parameters in the c# programming language.
If you observe the above example, we created a method called “GetUserDetails” without having any return type (void) and parameters.
When we execute the above c# program, we will get the result below.
This is how we can create methods in c# without having any return type and parameters based on our requirements.
If we create a method with parameters in c#, then we need to pass parameters to that method while calling it in our application.
In c#, we have different ways to pass parameters to the method; those are
Parameters | Description |
---|---|
Value Parameters | These are called “input parameters” and these parameters will pass a copy of the original value instead of the original parameters. So the changes made to the parameters in the called method will not affect the original values when control returns to the caller. |
Reference Parameters | These are called “input/output parameters” and these will pass a memory reference of original parameters. So the changes made to the parameters in the called method will affect the original values when control returns to the caller. |
Output Parameters | These are called “output parameters” and these are more like reference type parameters, but the only difference is we don’t need to initialize them before passing. |
In the next chapters, we will learn more about parameter passing to methods or functions in a detailed manner with examples in the c# programming language.