C#
  Home arrow C# arrow Value Types and Reference Types
Iron Speed
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM Developerworks
 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: 5 stars5 stars5 stars5 stars5 stars / 2
    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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    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

    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained
    - A Look at C# File and FileInfo Classes
    - A Look at C# Directory and DirectoryInfo Cla...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway