Understand the String Type in C #

zhaozj2021-02-16  48

purpose

The purpose of this article is to reveal some common and uncommon issues related to DOTNET and C #. In these issues, my first article and String data type are related, String data types are a reference type, but when compared to other reference types, many developers may not fully understand its behavior.

problem

For common reference types, this change is also exhibited in an actual object when changing a value of an object alias; vice versa. But for the String type, it doesn't seem like this.

Explanation

Reference type

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

Below, let's take a 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 reason 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 = SadiqValues ​​Of Obj1 = Ahmed and Obj2 = AHMED

This indicates that Obj1 is just Obj2 alias. In other words, OBJ1 and OBJ2 are all point to the same memory space.

Value type

And the above code is almost, the difference is this time we define mytype as classes, all other parts are the same, let's take a 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 reason 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 running on the above code:

***** Learning Reference Philosophy ***** Values ​​Of Obj1 = Sadiq and Obj2 = SadiqValues ​​Of Obj1 = Ahmed and Obj2 = Sadiq This indicates that Obj1 and Obj2 are different, that is, they point to different memory spaces.

Quote type or value type? Now let's take a look at the situation directly using the String type:

Using system;

Class apptype

{

Public static void main ()

{

String Obj1, obj2;

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

// No NEED OF IT

// Obj2 = new mytype ();

Obj2 = "sadiq";

Obj1 = Obj2;

Console. WriteLine ("VALUES OF IBJ1 = {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 = SadiqValues ​​Of Obj1 = Ahmed and Obj2 = Sadiq

This indicates that OBJ1 is not an alias of OBJ2, that is, OBJ1 and OBJ2 points to different memory spaces.

Very strange! It is true! We all know that the String type is dynamic growth, which indicates that it must allocate memory on the heap. We all know that the reference types are allocated in the pile, then the string type should also be the reference type, then why do it show the same nature as the value type?

The reason is that the following two lines of code:

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

The first line of code is merely a object that does not create an object; the second line of code will really create an object. This means you can also write the second line of code:

Obj = new string ("Value Forces to Allocate a Memory) ;.

to sum up

therefore

,

When you initialize one

String

A new object will be created in memory when the value of an object or gives it to it. right now

,

We should understand the third example

Obj1

Not

Obj2

Alias

,

They point to different memory spaces.

转载请注明原文地址:https://www.9cbs.com/read-23442.html

New Post(0)