Working with Instance and Local variables

The variable created within the class is called “Instance variables”. The variables created within a function are called “local variables”. When instance variables names and local variable names are the same then by default priority will be given to the local variables In the above case in order to access instance variables this keyword is required. This keyword always points to the currents class. This will be created at run time. Example of “this” keyword with instance and local variables.

 arrrowPlace a button

arrrowCode in GD  

Code  

Class EMP

{

Private int Sal = 5000;

Public void incr (int Sal)

{

This Sal = this. Sal + Sal ;

}

Public void print ()

{

Message Box. Show (“Sal = ” + Sal)

}

Code for Button 1_ click

EMP e1 = new EMP ();

E1.incr (3500); E1.print ();

Interested in mastering .NET? Learn more about ".NET Training "in this blog post.

Function Arguments

Syntax

<Access Specifier>return type func _name (args) C#

.net allows to pass the arguments in three ways:-

Call by value

Call by reference

Call by out  

Call by value and call by reference

When formal arguments are modified and if modification is reflected on actual arguments, then the concept is called as “call by reference” When formal arguments are modified and if modification are not reflected on actual arguments, then the concept is called as   “call by value” By default variables will be passed by value. Reference is a keyword which is required to pass a variable The variables which are passing by ref must be initialized Reference keyword must be used along with actual and formal arguments.

Check Out .Net Tutorial

Example on call by value and call by reference

Open windows form application project

arrrowPlace a button

arrrowCode in GD

Class Test

Public void SWAP (int a, ref int b)

{

Int t = a;

A= b;

B =t;

}

}

Code for Button 1 _click

Private void button1_click ()

{

Int x = 10;

y = 20;

Test t = new Test ();

t.SWAP (x, ref y);

Message Box. Show (x + “” + y);

}

Call by out

It is 99% similar to cal by ref. Out is a keyword Out allows to pass a variable without initialization also. Event if out variable is initialized, the value will not be passed. out = ref _ initialization  

Example on call by out

Place a button Code in GD  

Class test

{

Public void print (out int x)

{ X =10;

X= x +x;

}

}

Code for button 1_ click ()

Private void button1_ click ()

{

Int a ;

Test t = new Test ();

t.Print (out a);

Message Box. Show (a);

}