ReadOnly Fields in C#

"ReadOnly" members are just like constants depending on the objects. The "readonly" modifier can be used for fields only. Once you initialize the value of "ReadOnly data members", the object can't change that value, but it can access the value.
 
Note: The read only field's values can be changed from inside of the class only. The following describes how to declare read only fields.
 
Syntax
 

access_specifier readonly data_type variable_name = value;
 
Ref: Parameters
 

Reference parameters are similar to normal parameters. The only difference between normal parameters and reference parameters is that when the value is changed in the reference parameter, it would automatically affect the actual parameter in the calling portion. Implementation: use the "ref" keyword in the calling portion and also in the method definition.
 

Rule: The actual parameter at the calling portion should be a variable and can't be a constant. This is just like the "Call by reference" concept in C/C++.
 
Ref - Parameters 
 
Out: Parameters
 

This is to return multiple values from a method. The "out" parameter is similar to the "ref" parameter; but the difference between these two parameters is, the out parameter does not carry a value from the calling portion to the method definition; it only carries the return value to the calling portion.
 
Implementation: Use the out keyword in the method call and also in the method definition. So, the "out" variable need not be initialized when you call the method.
 
Out - Parameters 
 
This: Keyword
 

This is similar to the "this" pointer in C++. It represents the current working object. It is used to access the members of the current working object.
 
 this.field
 this.property
 this.method()
 
Current object: The object, with which object, the method is called. The "this” keyword can't be used in the static methods, because static methods don't have a "this" pointer.
this - Keyword 
 
Named Parameters
 

This feature is introduced in C#.NET 4.0. This is used to pass the arguments to the method, with those names.
 
Named Parameters 
 

Up Next
    Ebook Download
    View all
    Learn
    View all