×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Value type & Reference type

Value type & Reference type

Value type & Reference type

(OP)
What  is  the  difference  between  value  type  &  Reference  type  ?????


Can  any  body  explain  with  examples???????


RE: Value type & Reference type

Value types are simple types like enums, structs and datatypes like int, char. They are created on the stack and directly contain data. They are state based.

Reference types are complex types like classes, interfaces, delegates. They are created on the heap and are accessed indirectly by reference variables. They are behaviour based.

All types (both value and reference) inherit from OBJECT superclass. Primitive types can be cast to Ref. types by boxing them.

Refer Jeffrey Richters excellent article http://msdn.microsoft.com/library/periodic/period00/dotnet1200.htm

RE: Value type & Reference type

Value Type is the way classic C & C++ works when passing a variable to a function, without any specific modifiers. Primitive data types, enums, structs and even objects would fall under this category for C++ BUT IN C# only primitive data types, enums and structs are treated as VALUE TYPES.

Take an example in C#

using System;
class Test
{
   static void Main()
   {
      int x = 10;
      funCalled(x);
      Console.WriteLine(x); //will print 10
   }

   void funCalled(int x)
   {
      x = 200;
      /*This is value type, wherein the x in this function is a NEW variable created in the stack for this function only, which means that the value changed here will not be affecting the calling function*/
   }
}

BUT since JAVA came in place, reference types got introduced, in Java & C# OBJECTS are by default passed as REFERENCES

Take another example in C#

using System;

class SomeObject
{
   public int value;
}

class Test
{
   static void Main()
   {
      SomeObject x = new SomeObject();
      x.value = 10;
      funCalled(x);
      Console.WriteLine(x.value); //will print 200
   }

   void funCalled(SomeObject x)
   {
      x.value = 200;
      /*This is a refernce type, wherein the x in this function is only REFERRING to the same object in the memory, like an alias, which means that if the value is changed here, it will be affecting object in the the calling function*/
   }
}


A C++ programmer can think of reference types as the way we work with POINTERS TO OBJECTS


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close