At. NET (C # and therefore) there's one main types of type: reference types and value types. They act differently, as well as a lot of confusion about parameter passing is right up to people not understanding the difference between them. Here is a brief explanation:
A reference type is a type whose reference value at the appropriate data than the data itself. For example, consider the following code:
StringBuilder sb = new StringBuilder ();
I have used StringBuilder as a random example of a reference type - there is nothing special.) Here, they declare a variable SB, generate a new StringBuilder object, and assign the SB, a reference to the object. The value of SB is not the object itself, reference is. Allocation of participation reference types is simple - the value assigned is the value of the expression / variable - ie the reference. This will be shown in this example:
First = new StringBuilder StringBuilder ();
StringBuilder second = first;
Here they declare a first variable, generate a new StringBuilder object, and assign to first a reference to the object. Then assign the second value of the first. This means that both refer to the same object. They remain, however, the independent variables themselves. Alter the value of the first won't alter the value of the second - but simultaneously their values are still references the same object, changes made to the object through the first variable will be visible through the second variable. Here is a demonstration that:
First = new StringBuilder StringBuilder ();
StringBuilder second = first;
first.Append ( "hello");
first = null;
Console.WriteLine (second);
Here, first declare a variable, generate a new StringBuilder object, and assign to first a reference to the object. Then assign the second value of the first. They then call the Append technique on this object through the reference in the first variable. After this, they set the first variable to null (a value which does not refer to any object). Finally, they show the results of the ToString technique call the StringBuilder object through the reference in the second variable. Hello shown, demonstrating that although the first value has changed, the data within the object that is used to refer to non - and the latter still refers to that object.
Class types, interface types, delegate types and array types are all reference types.
0 comments:
Post a Comment