Tour rookie ------ C # value types, reference types

       Yesterday in learning design patterns, encountered a "prototype model", there is a very key point is that in this model: by value and pass-by. To address this issue our team came back to Xiao Yang video to learn a lot, it looks very clear, but back to the design mode after everyone's opinion is different.  

       So for value types and reference types that question, I launched a series of learning. Here are some of my harvest, for everyone to share.

       Type in C # into two types: value types and address type

 NOTE: values ​​belonging to the structure type

           All classes belonging to a reference type, comprising an interface.

Value type comprising: a structure numeric types, structures, bool type, user-defined, enumeration, nullable type.

Reference type comprising: an array of user-defined classes, interfaces, delegates, Object, string, null type, class.

What is the difference between value types and reference types are: 

        Examples of value type is assigned to a thread stack among all reference type instances are managed heap allocated

performance:

        Since the value type instance is of value itself, and cited examples of the type of value is a reference, so if a value type variable to another type of value

Variable, performs a copy-by-field, assign a reference type variable to another variable of reference type, only need to copy the memory address.

 

For example the following code: 

    class SomRef
    {
        public int x;
    }

    struct SomeVal {
        public int x;
    }

    class Program
  {
        static void ValueTypeDemo()
       {
            SomRef r1 = new SomRef();//在堆上分配
            SomeVal v1 = new SomeVal();//在栈上分配
            r1.x = 5;//提领指针
            v1.x = 5;//在栈上修改
            SomRef r2 = r1;//只复制引用(指针)
            SomeVal v2 = v1;//在栈上分配并复制成员
        }
    }

One more example:

   public class Person
   {
       public string Name { get; set; }// 定义个Name 属性
        public int Age { get; set; }//定义个Age 属性
    }
     
    public static class ReferenceAndValue
    {
        public static void Demonstration()
      {
         Person zerocool = new Person { Name = "ZeroCool", Age = 25 };
           Person anders = new Person { Name = "Anders", Age = 47 };
   
          int age = zerocool.Age;
          zerocool.Age = 22;
          Person guru = anders;// 这个用到了引用,引用了前面定义好的 类anders;
           anders.Name = "Anders  Hejlsberg";
         Console.WriteLine("zerocool's age:\t{0}", zerocool.Age);
           Console.WriteLine("age's value:\t{0}", age);
           Console.WriteLine("anders' name:\t{0}", anders.Name);
          Console.WriteLine("guru' name:\t{0}", guru.Name);
      }
   }

First, we define a Person class, this class is no doubt a reference type

Then, we declare two Person instance of an object class, zerocool and anders, mentioned earlier, these two objects are allocated on the heap, while zerocool and anders itself is only the starting address of the memory region where the object references, in other words a pointer here.

We declare a value type variable age, directly upon initialization of the Age value zerocool assigned to it, obviously, age 25 is the value of the

We declare the value type variable age and zerocool.Age assigned to it, the compiler on the stack allocated a space, and then the filling into account the value of zerocool.Age

But the reference type is not the same, when we declare guy put anders assigned to it, as I said before, is just a reference type contains a reference to the heap data area address, in fact, a reference to the anders also assigned to the guy therefore both from the point to the same memory area, since it is pointing to the same area, so no matter who moved inside the "cheese", another realization that the results will change along with

Final Results:

A special reference types: String

Yesterday, when we discuss the design mode, we met an acute problem, and that is of type String on the Internet that is also a reference type, but why its results and general references to different types of it?

Because it is a special reference type.

string and reference types there are some differences in common operations.

For example, the string is immutable. Modify a string, it will create a new string objects, and any other string does not change .

 

using System;

class StringExample
{

    public static int Main()
    {

           string s1 ="aaaa";

           string s2 = s1;

           Console.WriteLine("s1:" + s1);

           Console.WriteLine("s2:" + s2);

           s1 = "bbbb";

           Console.WriteLine("s1:" + s1);

           Console.WriteLine("s2:" + s2);

           return 0;
    }

}

输出结果:
s1: aaaa
s2: aaaa
s1: bbbb
s2: aaaa

Changing the value of s1 s2 no influence, this type of operation with reference to the contrary , when using "aaaa" Initialization s1, allocated on the heap in a new string object. In s2 initialization, references are also pointing to the object, the value of s2 is "aaaa", but when the value of s1, and will not replace the original value , the heap will assign a new value to a new string object

Then there are still people doubt me, so I drew a diagram to explain this phenomenon:

After the partners believe there is an array of strings and effects are the same, then we have to solve through a bit of code:
 

static void Main(string[] args)
        {
            int[] s1 = new int[] { 1, 2, 3 };

            int[] s2 = s1;

            Console.WriteLine("s1:{0}{1}{2}", s1[0], s1[1], s1[2]);

            Console.WriteLine("s2:{0}{1}{2}", s2[0], s2[1], s2[2]);

            s1[1] = 4;

            Console.WriteLine("s1:{0}{1}{2}", s1[0], s1[1], s1[2]);

            Console.WriteLine("s2:{0}{1}{2}", s2[0], s2[1], s2[2]);

            Console.ReadKey();
        }


最终显示结果
s1:123
s2:123
s1:143
s2:143

Obviously the result is not the same, so we can use a general reference type "array" to bring out the special reference type "string", they are different

note:

static void StrChange (string str) are passed by value

 

static void StrChange (ref string str) are passed by reference

Guess you like

Origin blog.csdn.net/qq_30631063/article/details/86550140