GST Billing Software

header image

Working with Parameters in C#

Erachana Line

The parameter is used to passing information to a function. Based on the type of parameters, function responses, and returns value. A parameter can be passed to a function by two ways: Passing parameter by value and passing a parameter by reference.

Function Parameter in C#

The parameter is the essential part of the function. However, it is optional but it makes the C# function more dynamic than simple function. There are two type of function parameter in C#, Value type parameter and reference type parameter

C# parameter is just a way to passing value to the function and then function calculate the value and then returns appropriate results. You can understand C# function parameter clearly in the following example.

Programming Example

1. using System;

2. using System.Collections. Generic;

3. using System.Linq;

4. using System.Text;

5.

6. namespace Understanding_Parameter

7. {

8. class Program

9. {

10. // function with parameter

11. public static int power(int num1)

12. {

13. int result;

14. result = num1 * num1;

15. return result;

16. }

17.

18. static void Main(string[] args)

19. {

20. int pow;

21. // passing arguement as parameter

22. pow = Program.power(5);

23. Console.Write("\nPower = {0}", pow);

24. Console.ReadLine();

25. }

26. }

27. }

Output

Power = 25

__

While using parameterized function, must declare valid return data type with function as follow:
For Integer value:

1. public int power(int num1)


For String value:

1. public string print(string name)


You can also return appropriate value with return keyword.

Value Type Parameter In C#

As mentioned in the previous chapter, there are two types of the parameter that can be passed in function. One is Value type parameter and another is reference type parameter. Before understanding both types of parameters in C#, you will have to understand Value type and Reference type.

There are two ways to allocating space in memory. One is a value type and another is a Reference type. When you create int, char or float type variable, it creates value type memory allocation whereas when you create an object of a class, it creates reference type memory allocation.

Value Type: A value type variable directly contains data in the memory.
Reference Type: A Reference type variable contains memory address of value.
Consider the following graph to make better sense of value type and reference type.

1. int Result;

2. result=200;


valuetype-parameter-flowchart

In the preceding example, the value type variable contains the value whereas a reference type variable contains the address of Result variable.

VALUE TYPE PARAMETER:

In value type parameter, the actual value gets passed to the function. Passing a value type variable as parameter means, you are passing the copy of the value.

Programming Example

1. using System;

2. using System.Collections. Generic;

3. using System.Linq;

4. using System.Text;

5.

6. namespace Value_Type

7. {

8. class Program

9. {

10. public static int qube(int num)

11. {

12. return num * num * num;

13. }

14. static void Main(string[] args)

15. {

16. int val, number;

17. number = 5;

18. //Passing the copy value of number variable

19. val = Program.qube(number);

20. Console.Write(val);

21. Console.ReadLine();

22. }

23. }

24. }

C# Reference Type Variable

The Reference type variable is such type of variable in C# that holds the reference of memory address instead of value. class, interface, delegate, array are the reference type. When you create an object of the particular class with new keyword, space is created in the managed heap that holds the reference of classes.

If you are passing reference type variable as parameter, then you will have to use ref keyword with variable.

Programming Example

1. using System;

2. using System.Collections. Generic;

3. using System.Linq;

4. using System.Text;

5.

6. namespace Reference_Parameter

7. {

8. class Program

9. {

10. public static void qube(ref int num)

11. {

12. num = num * num * num;

13. }

14. static void Main(string[] args)

15. {

16. int original = 9;

17. Console.Write("\ncurrent value of Original is {0}\t", original);

18. Program.qube(ref original);

19. Console.WriteLine("\nNow the current value of Original is {0}\t", original);

20. Console.ReadLine();

21. }

22. }

23. }


Output

current value of Original is 9

Now the current value of Original is 729

valuetype-parameter-flowchart