C# Method
C# Method
A method is a block of code that performs a specific task. Suppose you need to create a program to create a circle and color it. You can create two methods to solve this problem:
a method to draw the circle
a method to color the circle
Dividing a complex problem into smaller chunks makes your program easy to understand and reusable.
Declaring a Method in C#
Here's the syntax to declare a method in C#
Here,
returnType - It specifies what type of value a method returns. For example, if a method has an
int
return type then it returns anint
value. If the method does not return a value, its return type isvoid
.methodName - It is an identifier that is used to refer to the particular method in a program.
method body - It includes the programming statements that are used to perform some tasks. The method body is enclosed inside the curly braces
{ }
Let's see an example,
Here, the name of the method is display()
. And, the return type is void
Calling a Method in C#
In the above example, we have declared a method named display()
. Now, to use the method, we need to call it.
Here's how we can call the display()
method.
In the above example, we have created a method named display()
. We have created an object student of the Student class.
Notice the line,
student.display();
Here, we are using the object to call the display()
method.
C# Method Return Type
A C# method may or may not return a value. If the method doesn't return any value, we use the void
keyword (shown in the above example).
If the method returns any value, we use the return statement to return any value. For example,
Here, we are returning the variable sum
. One thing you should always remember is that the return type of the method and the returned value should be of the same type.
In our code, the return type is int
. Hence, the data type of sum
should be of int
as well.
Example: Method Return Type
Note: As the method is static we do not create a class object before calling the method. The static method belongs to the class rather than the object of a class.
C# Methods Parameters
In C#, we can also create a method that accepts some value. These values are called method parameters. For example,
Here, a
and b
are two parameters passed to the addNumber()
function.
If a method is created with parameters, we need to pass the corresponding values(arguments) while calling the method. For example,
Here, We have passed 2 arguments (100, 100)
.
Example 1: C# Methods with Parameters
Built-in Methods
So far we have defined our own methods. These are called user-defined methods.
However, in C#, there are various methods that can be directly used in our program. They are called built-in methods. For example,
Sqrt()
- computes the square root of a numberToUpper()
- converts a string to uppercaseExample: Math.Sqrt() Method
Method Overloading in C#
In C#, we can create two or more methods with the same name. It is known as method overloading. For example,
In the above example, we have overloaded the display()
method. It is possible because:
one method has one parameter
another has two parameter
Last updated