C#中字符串的比较 [英] comparsion of string in C#

查看:69
本文介绍了C#中字符串的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的程序



a simple program

string s1 = "Faizan";
           string s2 = "Faizan";
           if (s1 == s2)
           {
               Console.WriteLine("yes");
           }
           else {
               Console.WriteLine("no");
           }
           Console.ReadLine();





令人惊讶的是它对我显示是。为什么字符串是对象类型为什么它是相等的?



surprisingly it is show yes to me. why string are object type the why it is equal?

推荐答案

在C#中,每个不同的字符串值都存储并且是不可变的。

两个变量都指向内存中相同的字符串,因此得到相同的结果。



更多细节可用此处 [ ^ ]。
In C#, each distinct string value is stored and is immutable.
Both variables point to the same string in memory and thus you get an equal result.

More detail available here[^].


您可能知道,在.NET中,您不能说一切都从Object继承[ ^ 例如,接口不会从Object继承。



继承自.NET中的Object的两种基本类型是System.ValueType和System。 ReferenceType ...然后有Integral Types,比如Int32,它们是ValueType的子类(Integral Types是Structs,Structs继承自ValueType继承自Object)。



虽然说话很常见所有ValueTypes存储在堆栈中,而ReferenceTypes存储在堆中,现实并不那么明确;我建议你阅读Eric Lippert关于Stack的这两篇文章作为实现细节:[ ^ ],[ ^ ]。



Type String是ReferenceType的一个特例:String是一个Immutable ReferenceType;一旦创建它就无法更改[< a href =http://3.bp .blogspot.com / _gez10dNhuPk / Rb4IV-mHxVI / AAAAAAAAADk / viWcrNAGkCA / s400 / 3 +不可变+参考+ Types.giftarget =_ blanktitle =新窗口> ^< / a>]。 />


对于Type String,=和!=运算符被重载,因此使用其内容中的Value(字符)进行比较,而不是通过它们引用的内容进行比较(内存地址他们指向。)C#语言设计如何实现字符串(我读过)就像在Java中那样。



所以你可以说Type String作为一个引用类型,作为Immutable,并且具有值语义:你使用它的方式就像你使用值类型的方式。



在.NET中定义字符串的方式很简单:效率,速度,优化内存要求。字符串的不变性意味着如果在应用程序中多次重复使用相同的字符串文字,编译器将不会复制字符串数据内容,而只会将其值(字节数组)存储在内存中一次。



不可变性意味着每次对String进行操作时,都会更改其内部值,并生成String的副本;这就是为什么像Trim,SubString这样的String方法返回一个副本而不是修改它们被调用的String。
As you may know, in .NET you can't say that "everything inherits from Object" [^]: Interfaces, for example, do not inherit from Object.

The two fundamental flavors of Types that inherit from Object in .NET are System.ValueType , and System.ReferenceType ... then there are the Integral Types, such as Int32, which are a sub-species of ValueType (Integral Types are Structs, Structs inherit from ValueType inherit from Object).

While it's common to speak of all ValueTypes being stored on the Stack, and ReferenceTypes being stored on the Heap, the reality is not so clear-cut; I recommend you read these two articles by Eric Lippert about the Stack as "implementation detail: [^], [^].

Type String is a special case of ReferenceType: a String is an Immutable ReferenceType; it can't be changed once it is created [<a href="http://3.bp.blogspot.com/_gez10dNhuPk/Rb4IV-mHxVI/AAAAAAAAADk/viWcrNAGkCA/s400/3+Immutable+Reference+Types.gif" target="_blank" title="New Window">^</a>].

For Type String, the = and != operators are overloaded so they are compared using the Value (characters) in their content, rather than being compared by what they reference (the memory address they "point-to"). The C# language design of how Strings are implemented is (I have read) like that in Java.

So you can speak of Type String as a Reference Type, as Immutable, and having "value semantics:" the way you use it is like the way you use Value Types.

The reasons for the way Strings are defined in .NET are simple: efficiency, speed, optimizing memory requirements. The Immutability of Strings means that if you re-use the same String Literal many times in your Application, the Compiler will not duplicate the String data-contents, but only store its values (byte array) in memory once.

Immutability means that every time you perform an operation on a String that changes its internal value a copy of the String is made; this is why String methods like Trim, SubString return a copy rather than modify the String they are called on.


这篇关于C#中字符串的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆