The difference between a c#string and a string

Source: Internet
Author: User

C # is case-sensitive, but I find that there are strings in C # and string, so I was confused, so I searched the internet, so I learned some small knowledge.

MSDN: string is an alias for String in the. NET Framework. string is a string alias, string is a class in C #, ,c# string is mapped to the framework String

If you use C #, it is recommended that you use string to compare to specifications . string always represents System.String (1.x) or:: System.String (2.0), string is only preceded by using System, and there is no type named string in the current namespace (class, struct, delegate, enum) is only representative of System.String.

String is a keyword, string is not, that is, string cannot be the name of a class, struct, enumeration, field, variable, method, property, and string.

today, using the C # string, I suddenly remembered that I had not figured out the difference between string and string, checked the ebook, MSDN, and Baidu and Google, and took a closer look at some of the problems with string.  0. Question: 1. When does C # actually pass a quote? When is the value passed? 2. String to pass the value or reference 3. What is the difference between a string and a string? 4. Why is string immutable, and how is it implemented? The following query results and my understanding: 1. When does C # actually pass a quote? When is the value passed? case of a value: Struct, enumeration, Numeric (integral/floating/decimal), bool Reference case: Class, Delegate, Interface when using operator "="        As well as the function arguments: The result of the pass value is to copy the original object, and the receiver points to the original object. The result of a pass-through reference is to direct the receiver to the original object. Some people say, I am hard to put worth quote what to do? A, with the REF keywordb, using arrays, array is classC, salad:) 2. A string declaration that refers to C # is a class string and is, of course, a reference. However, the reason for this confusion is mostly because of the situation:string a = "AAA";string b = A;B = "BBB";or a few lines of code:Public void Swap (string s1, string s2){string temp=s1;s1=s2;s2=temp;}At this time the results of a printing, the results found that a value has not changed, swap has not been successful, this time there will be hallucinations: is not a reference ah? Oh, the string will not be so rude to disrupt the "declared as class is a reference" rule. Analysis:string a = "AAA";//==> a----->new string ("AAA")string b = A; ==> b----->a, pass referenceB = "BBB"; ==> b----->new string ("BBB"), references, B points to a new string, and a does not change. The swap function is also like this, such as passing A, b in (a= "AAA", b= "BBB"),//s1----->a, s2----->bstring temp=s1;//temp----->s1----->as1=s2; S1----->S2----->b;s2=temp; S2----->temp----->aAs a result, S1 and S2 are indeed swap, but the results do not affect A and B 3. What is the difference between string and string? description of String on MSDN: string is a alias for string in the. NET Frameworkhehe string is the alias of String, is a. 4. Why is string immutable, and how is it implemented? immutable: Once an object is generated, it cannot be changedabout how to achieve, after understanding the problem 2 is very good, as long as not to provide any modification of their member variables of the method can be. By way of sealed, to prevent unclear subsequent violation of the regulations:)string Each of the methods that appear to modify the member variable actually returns a new string. For example, the String.Replace function does not actually change the original string, which is why only str = STR is allowed. Replace ( foo, bar) is the reason for the replacement to really complete.

The difference between a c#string and a string

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.