In C#, parameter modifiers are keywords or attributes applied to method parameters to specify certain behaviors or constraints. There are several parameter modifiers available in C#:
ref: Indicates that a parameter is passed by reference, allowing the method to modify the value in the calling method. (An example is provided in the code below. Through âref,â if there is a value initialized similar to C++ addresses, the method can change that value from inside or outside. However, the variable must already have a value initialized. Must have value initialized. āύāĻŋāĻā§āϰ āĻā§āĻĄā§ āĻāĻĻāĻžāĻšāϰāύ āĻāĻā§āĨ¤ āϰā§āĻĢ āĻāϰ āĻŽāĻžāϧā§āϝāĻŽā§ C++ āĻāϰ āĻāĻĄā§āϰā§āϏā§āϰ āĻŽāϤ⧠āĻā§āύ⧠āĻā§āϝāĻžāϞ⧠initialized āĻāϰāĻž āĻĨāĻžāĻāϞ⧠āϤāĻžāϰ value change āĻāϰāϤ⧠āĻĒāĻžāϰ⧠method āĻĨā§āĻā§ āĻŦāĻž āĻŦāĻžāĻāϰ⧠āĻĨā§āĻā§āĨ¤ āϤāĻŦā§ āĻāĻā§ āĻĨā§āĻā§āĻ veriable āĻ āĻā§āϝāĻžāϞ⧠āĻĨāĻžāĻāϤ⧠āĻšāĻŦā§āĨ¤ āĨ¤ Must have value initialized) out: Similar to âref,â used for output parameters, indicating that the parameter does not need to be initialized before being passed. (An example is given in the code below. Through âout,â it can also initialize a variable similar to C++ addresses, which will get its value assigned from outside. However, the variable cannot have a value initialized beforehand. Never have value initialized. āύāĻŋāĻā§āϰ āĻā§āĻĄā§ āĻāĻĻāĻžāĻšāϰāύ āĻāĻā§āĨ¤ āĻāĻāĻ āĻāϰ āĻŽāĻžāϧā§āϝāĻŽā§āĻ C++ āĻāϰ āĻāĻĄā§āϰā§āϏā§āϰ āĻŽāϤ⧠āĻā§āύ⧠variable āĻ value initialized āĻāϰāϤ⧠āĻĒāĻžāϰ⧠āϝāĻž āĻā§āϰāĻŋā§ā§āĻŦāϞ⧠āϏā§āĻ āĻšā§ āĻŦāĻžāĻāϰ⧠āĻĨā§āĻā§āĨ¤ āϤāĻŦā§ āĻāĻā§ āĻĨā§āĻā§āĻ veriable āĻ āĻā§āϝāĻžāϞ⧠āĻĨāĻžāĻāϤ⧠āĻĒāĻžāϰāĻŦā§ āύāĻžāĨ¤ Never have value initialized) params: Allows a variable number of arguments of the same type to be passed to a method using an array. this: Used in extension methods to indicate the type that the method operates on. Keep in mind, No, the âthisâ keyword cannot be used within a static method in C#. āĻŽāύ⧠āϰāĻžāĻāĻŋ, No, the 'this' keyword cannot be used within a static method in C# in (C# 7.2 and later): Indicates that a parameter is read-only, optimizing for performance and preventing modification within the method. (In is only readonly) |
// ..............Normal method................
int x = 10;
SimpleMethod(x);
Console.WriteLine("Ans of simple method: " + x); // Ans: 10
static void SimpleMethod(int parameter)
{
parameter += 20;
}
// ..............ref................
int y = 200;
RefMethod(ref y);
Console.WriteLine("Ans of ref: " + y); // Ans: 500
static void RefMethod(ref int parameter)
{
parameter += 300;
}
// ..............out................
int z;
OutMethod(out z);
Console.WriteLine("Ans of out: " + z); // Ans: 7000
static void OutMethod(out int parameter)
{
parameter = 7000;
}
// ..............params................
int sum1 = Add(1, 2, 3, 4, 5);
int sum2 = Add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Console.WriteLine("Ans: " + sum1); // Ans: 15
Console.WriteLine("Ans: " + sum2); // Ans: 55
static int Add(params int[] parameters)
{
int result = 0;
foreach (var x in parameters)
result += x;
return result;
}
// ..............in................
int x = 10;
InMethod(in x);
static void InMethod(in int parameter)
{
// parameter += 20; // We can't do that because in is just readonly
Console.WriteLine(parameter);
}
// ..............this................
using System;
class MyClass
{
private int myNumber;
public MyClass(int number)
{
this.myNumber = number;
}
public void DisplayNumber()
{
Console.WriteLine("My number is: " + this.myNumber);
}
}
class Program
{
static void Main(string[] args)
{
MyClass obj = new MyClass(10);
obj.DisplayNumber();
}
}