Understanding string Types in C #

Source: Internet
Author: User
Objective

The purpose of this article is to uncover some common and uncommon problems associated with dotnet and C #. In these questions my first article has to do with string data types, the string data type is a reference type, but many developers may not fully understand its behavior when compared to other reference types.

Problem

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

Explain

Reference type

Suppose we have a class MyType, this class has an attribute name; We also have a class Apptype, which provides the main () method to run the program.

Next, 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 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 piece of 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 means that obj1 is just an alias for Obj2, in other words, obj1 and Obj2 both point to the same memory space.

Value type

Similar to the above code, the difference is that this time we define mytype as a class, and the rest are the same, so let's look at the code first:

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 of the above code after running:

Learning Reference philosophy*****
Values of Obj1=sadiq and Obj2=sadiq
Values of obj1=ahmed and Obj2=sadiq

This means 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 piece of code, you get:

Learning Reference philosophy*****
Values of Obj1=sadiq and Obj2=sadiq
Values of obj1=ahmed and Obj2=sadiq

This indicates that obj1 is not an alias for Obj2, that is, obj1 and Obj2 point to different memory spaces.

Very strange! We all know that the string type is dynamically growing, indicating that it must allocate memory on the heap. As we all know that the reference type allocates memory on the heap, then the string type should also be a reference type, so why does it exhibit the same properties as the value type?

Reason
The key is in the following two lines of code:

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

The first line of code simply defines an object, does not create an object, and the second line of code actually creates an object. This means that you can also write the second line of code:

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

Summarize

Therefore, when you initialize the value of a string object or give it a new string, a new object is created in memory. Now, we should understand that the third example of Obj1 is not a obj2 alias, they point to a different memory space.


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.