Regular Parameter Passing
This is passing parameters with no modifying keywords :
void MyMethod(Student studentObj, int aNumber) { aNumber += 5; studentObj.Name = "Jon"; }
In the above example MyMethod takes two parameters – a Student object and an Integer. Note the difference between the passing a Value Type (such as an Integer) and a Reference Type (such as any class such as a ‘Student’ object in the above example). Any operations performed on a value type will no be reflected in the original variable whereas operations performed on a reference type are reflected in the original variable. For example, assuming calling the above MyMethod method note the below code (note the comment explanation at the end of the code block):
int myInt = 8; Student aStudent = new Student(); aStudent.Name = "Mike"; MyMethod(aStudent, myInt); // myInt value is still 8 since it is a Value Type and immutable // aStudent.Name is now Jon since it is a Reference Type // and the variable aStudent just points to a Student object // on the Managed Heap which is then operated upon by the MyMethod(0 method.
Parameter Passing By Reference
By default all paramteres are passed by value – i.e. only the value of the variable is passed to method and the object itself is not passed. Thus operations within the method can only operate on the value of the parameter it receives and cannot make changes to the original variable (do note that this only applies to Value Types as noted above since Reference Types will have their state changes when they are passed to methods).
Alternatively, parameters can be passed by Reference using the ref keyword and their values will be changed by the method:
Alternatively, parameters can be passed by Reference using the ref keyword and their values will be changed by the method:
void MyMethod(ref int aNumber) { aNumber += 5; } protected void Page_Load(object sender, EventArgs e) { int myInt = 8; MyMethod(ref myInt); //myInt is now 13 }
In the above code the aNumber parameter in MyMethod now has the ref keyword, note that the ref keyword is always required when calling the method.
The integer which is passed to the method now has its value changed by the method.
The integer which is passed to the method now has its value changed by the method.
Parameter Passing Using the out Keyword
The out keyword has a similar impact to the ref keyword as the parameters that are passed to it can have their values changed.
The primary difference is that when using the out keyword the objects do not need to be initialized. In the above example of passing by reference there would be a compile-time error if the integer had simply been declared by not initialized with a value as below:
The primary difference is that when using the out keyword the objects do not need to be initialized. In the above example of passing by reference there would be a compile-time error if the integer had simply been declared by not initialized with a value as below:
int myInt;
If the ref keyword was replaced with the out keyword this issue would be resolved:
void MyMethod(out int aNumber) { aNumber = 3; aNumber += 5; } protected void Page_Load(object sender, EventArgs e) { int myInt; MyMethod(out myInt); //myInt is now 8 }
Note that the integer is now initialized with a value in the method body.
You might wonder what is the point of the out keyword. It can be very useful in situations where you want a method to perform several operations and return multiple values. In this circumstance you can simply declare multiple variables and pass them to void method to have their values assigned.
No comments:
Post a Comment