C#
  Home arrow C# arrow Value Types and Reference Types
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Value Types and Reference Types
By: Ayad Boudiab
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2008-04-30

    Table of Contents:
  • Value Types and Reference Types
  • Passing Value Types
  • Reference Types
  • Structures

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Value Types and Reference Types


    (Page 1 of 4 )

    C# is a language where every variable that you declare must have a type. A type is defined as a set of data and the operations performed on them. It is used when declaring local variables, classes, interfaces, arrays, structures and so on. When writing applications, you can use the types already provided by C#, like int, long, and System.Environment, or you can create your own (Employee, Book, Invoice). But in either case, the types come in two different flavors: value types and reference types.

    Value Types: A value type is either a simple type (bool,char,sbyte,byte,short,ushort,int,uint,long,ulong,float,double,decimal), astructtype, or anenumtype. A variable of a value type contains the data (as opposed to holding the address that shows where the data actually is). For example, the following declaration...


    intcount = 104;


    will be interpreted as:

    The memory location for the variable count contains the value 104.

    Assigning a value of value type to a variable of value type makes a copy of the value. Try the following code:


    class Program

    {

    static voidMain(string[] args)

    {

    intcount = 104;

    Console.WriteLine("Value of count is: {0}", count);


    intanotherCount = count;

    Console.WriteLine("Value of anotherCount is: {0}", anotherCount);

    }

    }


    This will result in the following:

    Seeing as structures and enumerations are value types as well, the same idea applies. Ponder the following example:


    class Program

    {

    static voidMain(string[] args)

    {

    Pointp;

    p.x = 2;

    p.y = 7;

    Console.WriteLine(p);

    Pointp2 = p;

    Console.WriteLine(p2);

    }

    }


    struct Point

    {

    public intx;

    public inty;

    public override stringToString()

    {

    return string.Format("[{0},{1}]", x, y);

    }

    }


    Notice that [2,7] is printed for both structures.

    More C# Articles
    More By Ayad Boudiab


     

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek