Saturday, March 1, 2014

ReadOnly Fields (ref, out, this, Named )

ReadOnly Fields

The "ReadOnly" members are just like constants according to 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 inside of the class only. To declare the read only fields:
Syntax:
           access_specifier  readonly data_type variable_name = value;



Readonly parameter

Download above code for Demo on ReadOnly Fields : Download Links

Ref - Parameters

The reference parameters are similar to the normal parameters. The only difference between the normal parameters and reference parameters is: When the value is  changed in the reference parameter, would automatically affect the actual parameter in the calling portion. Implementation: Use "ref" keyword in calling portion and also in method definition.
Rule:  The  actual  parameter  at  the  calling  portion  should  be  a  variable  and  can‘t  be  a
constant. This is just like "Call by reference" concept in C/C++.
ref parameter

Download above code for Demo on Ref Parameters Fields : Download Links

Out - Parameters

This is to return multiple values from a method. The "out" parameter is similar to "ref" parameter; but the difference between these two parameters  is,  out"  parameter  does  not  carries  any  value  from  calling portion  to  the method definition; but it carries the return value to the calling portion.
Implementation: Use ―out‖ keyword in calling portion and also in method definition. So, the "out" may not be initialized, when you call the method.
out parameter

Download above code for Out Parameters Demo :  Downloads Links

this - Keyword

This is similar to "this pointer" in C++. It represents current working object. It is used to access the members of current working object.

                         this.field
                         this.property
                         this.method()


Current object: The object, with which object, the method is called. "this” keyword can‘t be used in the static methods, because static methods doesn‘t have current object.
this keywords

Dowload above code for This Keyword Demo : Download Links

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 parameter

Download above Code for Named Parameters Demo : Downloads Links

No comments:

Post a Comment

Flag Counter