Understand the string type in C #

Source: Internet
Author: User

Purpose

This article aims to reveal some common and uncommon problems related to DOTNET and C. Among these questions, my first article 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 this program.

Next, let's look at the Code:

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.

Related Article

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.