O'Reilly logo

Special Edition Using C# by NIIT

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Value Types

Variables of the value type store the actual data. So, when the same data is accessed by two different functions, two copies of the data are created. This ensures that changes made to the data by one function are not reflected in the data accessed by the other.

Listing 5.4 shows an example.

Listing 5.4. Using Value and Reference Types
using System; 
class Firstclass 
{
          public char a=’a’; 
} 
class UsingFirstclass 
{
          static void Main() 
          {
          char b=’b’; 
          char c=b; 
          c=’c’; 
          Firstclass f1 = new Firstclass(); 
          Firstclass f2 = f1; 
          f2.a=’d’; 
          Console.WriteLine(“b ={0}”,b); 
          Console.WriteLine(“c ={0}”,c); 
          Console.WriteLine(“f1={0}”,f1.a); 
          Console.WriteLine(“f2={0}”,f2.a); 
          } 
}

The output of this program is as follows:

b=b 
c=c 
f1=d 
f2=d

In Listing 5.4 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required