• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Instance and Local variable in C# .Net

Working with Instance and Local variables

The variable created within the class are called as “Instance variables”.

The variables created within a function are called as “local variables”.

When instance variables names and local variable names are same then by default priority will be given to the local variables

In 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 on  “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 ();

 

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 are 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.

 

Example on call by value and call by reference

Open windows forms 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);

}

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Instance and Local variable in C# .Net"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.