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

Get Special Edition Using C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.