CODEDIGEST
Home Articles CodeDigest Tutorials FAQs
Skip Navigation LinksHome » Article » Csharp Article » Parameters in C# – Part 2   You are not logged in.
Search
 

Technologies
 

Sponsors
 

CodeDigest Navigation
 

Technology News
 

Community News
No News Feeds available at this time.
 
Parameters in C# – Part 2

By SelvaKumar
Posted On Jul 09,2008
Article Rating: (Login)
Be first to rate
this article.
No of Comments: 4
Print this article.
Category: C#

Subscribe to our feed!

Parameters in C# – Part 2

Reference Type

 

Introduction

Parameters in C#, the Part 2 of this article series will discuss on reference types and will help us to clear some of the confusions we have around this.

 

What is a reference type?

 

In .NET (and therefore C#) there are two main sorts of type: reference types and value types. They act differently, and a lot of confusion about parameter passing is really down to people not properly understanding the difference between them. Here's a quick explanation: A reference type is a type which has as its value a reference to the appropriate data rather than the data itself.

 

For instance, consider the following code:

 

StringBuilder sb = new StringBuilder();

 

(I have used StringBuilder as a random example of a reference type - there's nothing special about it.) Here, we declare a variable sb, create a new StringBuilder object, and assign to sb a reference to the object. The value of sb is not the object itself, it's the reference. Assignment involving reference types is simple - the value which is assigned is the value of the expression/variable - i.e. the reference.

This is demonstrated further in this example:

 

StringBuilder first = new StringBuilder();
StringBuilder second = first;

 

Here we declare a variable first, create a new StringBuilder object, and assign to first a reference to the object. We then assign to second the value of first. This means that they both refer to the same object. They are still, however, independent variables themselves. Changing the value of first will not change the value of second - although while their values are still references to the same object, any changes made to the object through the first variable will be visible through the second variable.

 



ASP.Net Hosting

Recent Articles

Here's a demonstration of that:

 

StringBuilder first = new StringBuilder();
StringBuilder second = first;
first.Append ("hello");
first = null;Console.WriteLine (second);

 

Sample Code

using System;
using System.Text;

public class Example1
{
    public static void Main (string[] args)
    {
        StringBuilder first = new StringBuilder();
        StringBuilder second = first;
        first.Append ("hello");
        first = null;
        Console.WriteLine (second);
    }

}

 

Output:
hello

 

Here, we declare a variable first, create a new StringBuilder object, and assign to first a reference to the object. We then assign to second the value of first. We then call the Append method on this object via the reference held in the first variable. After this, we set the first variable to null (a value which doesn't refer to any object). Finally, we print out the results of calling the ToString method on the StringBuilder object via the reference held in the second variable. hello is displayed, demonstrating that even though the value of first has changed, the data within the object it used to refer to hasn't - and second still refers to that object.

Class types, interface types, delegate types and array types are all
reference types.

 

Conclusion

In Part 2 of this article series, we have understood the Reference types and the confusion that exist around this. Going forward the Part 3 of this article will discuss on Value types in detail.

Thanks for reading this article!

 

Similar Articles
  • You can contribute to CodeDigest.Com:
    Article Feedback
    Title  
    Submitted By  
    Comment  
    Enter the verification number
     
    Comments
    LoCafc , [url=http://saotwlepipqd.com/]saotwlepipqd[/url], [link=http://gidjmtgwkznx.com/]gidjmtgwkznx[/link], http://rdrucrenazwb.com/
    LoCafc , [url=http://saotwlepipqd.com/]saotwlepipqd[/url], [link=http://gidjmtgwkznx.com/]gidjmtgwkznx[/link], http://rdrucrenazwb.com/
    T7DC9G <a href="http://jbuamxzojbux.com/">jbuamxzojbux</a>
    T7DC9G <a href="http://jbuamxzojbux.com/">jbuamxzojbux</a>
    MtsNw7 , [url=http://hboabvusabff.com/]hboabvusabff[/url], [link=http://gzsfqrvmtlpp.com/]gzsfqrvmtlpp[/link], http://iwqhfziamlfw.com/
    MtsNw7 , [url=http://hboabvusabff.com/]hboabvusabff[/url], [link=http://gzsfqrvmtlpp.com/]gzsfqrvmtlpp[/link], http://iwqhfziamlfw.com/
    This does work, you just need to pass IEnumerable as the generic T. But yes, you're right, you shulod be able to change it to directly return an IEnumerable by default, but I wasn't sure what would b
    This does work, you just need to pass IEnumerable as the generic T. But yes, you're right, you shulod be able to change it to directly return an IEnumerable by default, but I wasn't sure what would be best so I left it open.