Understand the string type in C #

Source: Internet
Author: User
This article aims to reveal some common and uncommon problems related to DOTNET and C. My first question Article It is related to the string data type. The string data type is a reference type, but when compared with other reference types, many developers may not fully understand its behavior.

Problem

For common reference types, when you change the value of an object alias, this change is also displayed in an actual object, and vice versa. However, this does not seem to be the case for the string type.

Explanation

Reference Type

Suppose we have a class mytype, which has a property name; we also have a class apptype, which provides the main () method to run thisProgram.

Next, let's take a lookCode:

Using system;

Class mytype

{

Private string name;

Public string name

{

Set

{

Name = value;

}

Get

{

Return name;

}

}

}

Class apptype

{

Public static void main ()

{

Mytype obj1, obj2;

Console. writeline ("****** learning reference philosophy *****");

Obj2 = new mytype ();

Obj2.name = "Sadiq ";

Obj1 = obj2;

Console. writeline ("values of obj1 = {0} And obj2 = {1}", obj1.name, obj2.name );

Obj1.name = "Ahmed ";

Console. writeline ("values of obj1 = {0} And obj2 = {1}", obj1.name, obj2.name );

}

}
 

When you compile and run this code, you will get the following output:

* ***** Learning reference philosophy *****
Values of obj1 = Sadiq and obj2 = Sadiq
Values of obj1 = Ahmed and obj2 = Ahmed

This indicates that obj1 is just an alias of obj2. In other words, both obj1 and obj2.

Value Type

Similar to the above Code, the difference is that this time we define mytype as a class, and other parts are the same. Let's first look at the Code:

Using system;

Struct mytype

{

Private string name;

Public string name

{

Set

{

Name = value;

}

Get

{

Return name;

}

}

}

Class apptype

{

Public static void main ()

{

Mytype obj1, obj2;

Console. writeline ("****** learning reference philosophy *****");

Obj2 = new mytype ();

Obj2.name = "Sadiq ";

Obj1 = obj2;

Console. writeline ("values of obj1 = {0} And obj2 = {1}", obj1.name, obj2.name );

Obj1.name = "Ahmed ";

Console. writeline ("values of obj1 = {0} And obj2 = {1}", obj1.name, obj2.name );

}

}
 

Let's take a look at the output after the code is run:

* ***** Learning reference philosophy *****
Values of obj1 = Sadiq and obj2 = Sadiq
Values of obj1 = Ahmed and obj2 = Sadiq

This indicates that obj1 and obj2 are not the same, that is, they point to different memory spaces.

Reference type or value type?
Now, let's take a look at the direct use of the string type:

Using system;

Class apptype

{

Public static void main ()

{

String obj1, obj2;

Console. writeline ("****** learning reference philosophy *****");

// No need of it

// Obj2 = new mytype ();

Obj2 = "Sadiq ";

Obj1 = obj2;

Console. writeline ("values of obj1 = {0} And obj2 = {1}", obj1, obj2 );

Obj1 = "Ahmed ";

Console. writeline ("values of obj1 = {0} And obj2 = {1}", obj1, obj2 );

}

}
 

When you run this code, you will get:

* ***** Learning reference philosophy *****
Values of obj1 = Sadiq and obj2 = Sadiq
Values of obj1 = Ahmed and obj2 = Sadiq

This indicates that obj1 is not the alias of obj2, that is, obj1 and obj2 point to different memory spaces.

Very strange! Yes! We all know that the string type is dynamically increasing, which indicates that it must allocate memory on the heap. We all know that the reference type is allocated memory on the stack, so the string type should also be the reference type, so why does it show the same nature as the value type?

Cause
The key lies in the following two lines of code:

String obj1;
Obj1 = "value forces to allocate a memory ";

The first line of code only defines an object and does not create an object. The second line of code truly creates an object. This means you can also write the second line of code as follows:

OBJ = new string ("value forces to allocate a memory ");.

Summary

Therefore, when you initialize the value of a string object or assign a new string to it, a new object will be created in the memory. Now, we should understand that the obj1 in the third example is not the alias of obj2. They point to different memory spaces.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.