Passing Parameters in C#
Parameters can be passed to a method in following three ways :
- Value Parameters
- Reference Parameters
- Output Parameters
Value Parameters copies the actual value of an argument into the formal parameter of the function. When a simple variable is passed as the parameter to any method, it is passed as a value. This means that the value contained by the variable that is passed as the parameter is copied to the variables of the method, and if inside the method these values are changed or modified, the change is not reflected in the actual passed variable. Most of the primitive data types such as integer, double, Boolean etc. are passed by value.
Reference Parameters copies the reference to the memory location of an argument into the formal parameter. Normally, all the objects are passed by reference as parameter to the method. The method operates on the references of the variables passed in the parameters rather than operating on their values. This results in the modification of variables in the calling function when they are modified in the called function. This means that changes made to the parameter affect the argument.
Output Parameters helps in returning more than one value . A return statement can be used for returning only one value from a function. However, using output parameters, you can return two values from a function. The variable supplied for the output parameter need not be assigned a value. Output parameters are particularly useful when you need to return values from a method through the parameters without assigning an initial value to the parameter. Output parameters are similar to reference parameters, except that they transfer data out of the method rather than into it.
- Does C# support multiple Inheritance ?
- What is Process ID ?
- How do I make a DLL in C# ?
- How many ways you can pass values to Windows Services ?
- Can we use "continue" statement in finally block ?
- What is nullable type in c# ?
- Difference between the Debug class and Trace class ?
- What is lock statement in C#
- What are dynamic type variables in C#
- What is the difference between is and as operator in C#?
- What are circular references in C#?
- What are the differences between events and delegates in C#
- Explain the types of unit test cases in C#?
- How many types of comments are there in C#?
- What are the different ways to deploy a assembly in net?
- What does assert() method do in c#
- What is literals in C# - Constants and Literals
- What is the use of goto statement in C#
- How can JIT code be faster than AOT compiled code
- Why events have no return types in .NET
- What's the difference between a static method and a non-static method in C#
- What's a weak reference c#?
- What is C# equivalent of the vb.net isNothing function
- What are indexers in C#
- What are generics in c#