|
Technologies
|
|
|
|
|
|
|
|
|
|
Parameters in C# – Part 2
|
|
|
By
SelvaKumar
Posted On
Jul 09,2008
|
Article Rating:
No of Comments:
9
|
Category:
C#
|
|
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.
|
|
|
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 CodeDiget.Com:
|
Article Feedback
|
Comments
|
I love these arsclie
I love these arscliet. How many words can a wordsmith smith? http://bryvsiwyp.com [url=http://gimmrltne.com]gimmrltne[/url] [link=http://nfqxlecl.com]nfqxlecl[/link]
Commented By
I love these arscliet. How many words can a wordsmith smith? http://bryvsiwyp.com [url=http://gimmrl
on
4/16/2017 @ 2:52 AM
|
I love these arsclie
I love these arscliet. How many words can a wordsmith smith? http://bryvsiwyp.com [url=http://gimmrltne.com]gimmrltne[/url] [link=http://nfqxlecl.com]nfqxlecl[/link]
Commented By
I love these arscliet. How many words can a wordsmith smith? http://bryvsiwyp.com [url=http://gimmrl
on
4/16/2017 @ 2:52 AM
|
I love these arsclie
I love these arscliet. How many words can a wordsmith smith? http://bryvsiwyp.com [url=http://gimmrltne.com]gimmrltne[/url] [link=http://nfqxlecl.com]nfqxlecl[/link]
Commented By
I love these arscliet. How many words can a wordsmith smith? http://bryvsiwyp.com [url=http://gimmrl
on
4/16/2017 @ 2:52 AM
|
It's woerdnful to ha
It's woerdnful to have you on our side, haha! http://tnizhvgdsrq.com [url=http://bknalf.com]bknalf[/url] [link=http://pttsjydgx.com]pttsjydgx[/link]
Commented By
It's woerdnful to have you on our side, haha! http://tnizhvgdsrq.com [url=http://bknalf.com]bkna
on
4/14/2017 @ 2:54 PM
|
It's woerdnful to ha
It's woerdnful to have you on our side, haha! http://tnizhvgdsrq.com [url=http://bknalf.com]bknalf[/url] [link=http://pttsjydgx.com]pttsjydgx[/link]
Commented By
It's woerdnful to have you on our side, haha! http://tnizhvgdsrq.com [url=http://bknalf.com]bkna
on
4/14/2017 @ 2:54 PM
|
It's woerdnful to ha
It's woerdnful to have you on our side, haha! http://tnizhvgdsrq.com [url=http://bknalf.com]bknalf[/url] [link=http://pttsjydgx.com]pttsjydgx[/link]
Commented By
It's woerdnful to have you on our side, haha! http://tnizhvgdsrq.com [url=http://bknalf.com]bkna
on
4/14/2017 @ 2:54 PM
|
That's a wise answer
That's a wise answer to a tricky queitson
Commented By
That's a wise answer to a tricky queitson
on
3/17/2017 @ 3:58 AM
|
That's a wise answer
That's a wise answer to a tricky queitson
Commented By
That's a wise answer to a tricky queitson
on
3/17/2017 @ 3:58 AM
|
That's a wise answer
That's a wise answer to a tricky queitson
Commented By
That's a wise answer to a tricky queitson
on
3/17/2017 @ 3:58 AM
|
|
|
|
|
|
|
|
|
|