Link Search Menu Expand Document

C Sharp Methods

Storing a series of statements into a single module reduces the drafting of the same code time and again or makes repeated use of a particular operation. In C#, the method is a separate code block containing several statements for specific operations. Scroll down to know more about the different methods in C Sharp.

What are Methods in C#?

Methods are a group of statements that can be assembled for the performance of a particular operation and return the results to the caller method portion. It also contributes to the reusability of code. Each C# software has at least one common Main Method ().

Methods are generally useful for improving the reusability of programming by avoiding repetition. If the same feature is available in other areas, we can build a method with the features needed to use it anywhere the application requires.

Methods Syntax

C# shall declare methods by defining the access level, return type, method name, and any parameters in a class or struct.

class class_name
{
<Access_Specifier> <Return_Type> Method_Name ()
{
// Statements
}
}

Method Calling

Method calling is performed when the user wants to use the method. You should use the method for its features. A method can be called multiple times.

Ex: static void MyMethod()

{
Console.WriteLine(“Calling a method”);
}
static void Main(string[] args)
{
MyMethod();
}

Characteristics of a Method in C#

  • Access level

Access modifiers regulate the access level of methods by Access Modifiers. They define the visibility of methods. Public, Private, Protected is the three different access modifiers in C#.

  • Return Parameter

It decides what type of data a method will return. The return type is void if the method does not return anything.

  • Method Name

This syntax section defines the name of the method, which is used to call the whole method.

  • Method Body

It specifies the line of code that performs the particular procedure or task for which the process is constructed. It is confined to curly braces.

  • List of parameters

They are true identification distinguished by the delimiter comma used for entering various types of parameters and preceded by each data form. They are contained in parentheses. The function does not include a parameter in the parameter list as well.

Different Methods in C#

1. Parameter-less Method

The parameter-less method is a method that has no parameter. It’s a text block used for the declaration.

Ex:

using System;
namespace Methods
{
class ParameterLessMethod
{
protected void Message()
{
Console.WriteLine(“Hello”);
}
static void Main()
{

var p = new ParameterLessMethod();
p.Message();
Console.ReadLine();
}

2. Parameterized Method

The parameterized method is considered as a method with at least one or more parameters.

Ex:

using System;
namespace Methods
{
class ParameterMethod
{
public void PrintMessage(string message)
 
{
Console.WriteLine(“Hello” + message);
}
static void Main()
{
var p = new ParameterMethod();
p.PrintMessage(“Manasa”);
Console.ReadLine();
}

3. Parameter and Return Method

The void type is provided as the return type by a method that does not return a value.

Ex:

using System;
namespace Methods
{
class ParameterAndReturnMethod
{
protected int MethodValue(int value)
{
Console.WriteLine(“Value is:” +value);
return value;
}
static void Main()
{
var p = new ParameterAndReturnMethod();
p.MethodValue(22);
Console.ReadLine();
 
}

4. Call by value

A copy of the original value will transfer the value parameters to the function instead of a reference. The original value is not changed or modified. A transferred value adjustment does not change the original value.

Ex:

using System;
namespace Methods
{
class CallByValueMethod
{
public void ShowValue(int value)
{
value *= value;
Console.WriteLine(“value:” +value);
}
static void Main(string[] args)
{
int value = 45;
var p = new CallByValueMethod();
Console.WriteLine(“value after calling method:” +value);
p.ShowValue();
Console.WriteLine(“value after calling method:” +value);
Console.ReadLine();
}

5. Reference Method

The ref keyword is used to transfer and return the reference value. Any shift of value passed as a reference often modifies and represents it.

Ex:

using System;
namespace Methods
{
class CallByReferenceMethod
{
protected void PrintNumber(ref int number)
{
number * = number;
Console.WriteLine(“ref method value:” + number);
}
static void Main()
{
int number = 219;
var p = new CallByReferenceMethod();
Console.WriteLine(“value before calling ref method:” +number);
p.PrintNumber(ref number);
Console.WriteLine(“Result after calling ref method:” +number);
Console.ReadLine();
}
}
}

6. Out parameter Method

The out keyword is used to transfer the argument as a parameter. It is the same as a reference form unless an initialization is necessary before the function is passed. It is helpful if several values are returned in a system.

Ex:

 

using System;
namespace Methods
{
class OutParameterMethod
{
protected void PrintNumber(out int number)
int square = 0;
number = square;
Console.WriteLine(“enter number”);
square = Convert.ToInt32(Console.ReadLine());
Console.WriteLine( “value:” +number);
}
static void Main()
{
int number = 55;
var p = new OutParameterMethod();
Console.WriteLine(value before:” +number);
p.PrintNumber(out number);
Console.WriteLine(value after:” +number);
Console.ReadLine();
}
}
}

7. Anonymous Method

An anonymous method is an unidentified inline code method. It is generated with the keyword of the delegate and does not include the name or type of return.

Ex:

 

using System;
namespace Methods
{
class AnonymousMethod
{
public delegate void Print(int value);
static void Main()
{
Print print = delegate(int val);
{
Console.WriteLine(“Inside method. Value: {0}”. val);
};
print(654);
Console.ReadLine();
}
}
}

8. Virtual Method

The Virtual Keyword is used to alter and override a process, property, indexing, or event statement in a derived class. The default implementation of a virtual method is often necessary. However, you should override the derived class, but not obligate. It can be overwritten with the keyword override.

9. Abstract Method

There is no implementation of an abstract form. It is located within the abstract class. The abstract approach is compulsory for the derived class. There is no need to use override keywords here.

10. Static Method

You can use the static keyword for a static method. The static approach is the procedure that can be named without generating a class object. The name of the class itself or the object of that class is used.

Ex:

 

using System;
namespace Methods
{
class StaticMethod
{
public static void PrintValue(int value)
{
Console.WriteLine(“value:” +value);
}
}
Public class Static
{
static void Main()
{
StaticMethod.PrintValue(333);
Console.ReadLine();
}
}
}

Other useful articles:


Back to top

© , C Sharp Online — All Rights Reserved - Terms of Use - Privacy Policy