C#对象? [英] C# objects?

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

问题描述

虽然读到C#中的所有内容都是一个对象,但为什么我能这样做:


string temp ="" ;;


而不是


string temp = new string("");


这只是编译器提供的快捷方式吗?

While reading that everything in C# is an object, why is it that I can do:

string temp = "";

instead of

string temp = new string("");

Is this just a shortcut the compiler provides?

推荐答案

你知道你可以用C#


(1).ToString() ;





(我的名字)。CompareNoCase(" Your Name")!= 0


甚至文字字符串或整数都是C#中的对象。


" Eddie Pazz" <博士**** @ hotmail.com>在消息中写道

新闻:e1 ************** @ TK2MSFTNGP10.phx.gbl ...
Do you know that you can do this in C#

(1).ToString();

or

("My Name").CompareNoCase("Your Name")!= 0

Even the literal string or integer are objects in C#.

"Eddie Pazz" <dr****@hotmail.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
在阅读所有内容的同时C#是一个对象,为什么我能这样做:

string temp ="" ;;

而不是

string temp =新字符串(");

这只是编译器提供的快捷方式吗?
While reading that everything in C# is an object, why is it that I can do:

string temp = "";

instead of

string temp = new string("");

Is this just a shortcut the compiler provides?



您好eddie,


字符串是特殊的并且有点像值类型(如int,

float ...)以及引用类型(其他所有内容)。


I不能确定C#是否以相同的方式行事(希望有人可以确认或否认这一点),但在Java中,字符串是在池中收集的,并且

重复使用,我相信C#也是如此。


string temp1 =" 123";

string temp2 =" 123" ;;


这将导致生成1个字符串并放入池中,并且两个

temp1和temp2将指向相同的字符串对象。


string temp1 = new string(" 123");

string temp2 = new string(" 123");


会导致2个字符串被生成,字符串不会被放入池中。


快乐编码!

Morten Wennevik [C#MVP]
Hi eddie,

strings are "special" and act a little bit like a value type (like int,
float ...) as well as a reference type (everything else).

I can''t say for sure if C# acts the same way (hopefully someone can
confirm or deny this), but in Java, strings are collected in a pool and
reused, and I believe C# does so too.

string temp1 = "123";
string temp2 = "123";

This would cause 1 string to be generated and put in the pool, and both
temp1 and temp2 would point to the same string object.

string temp1 = new string("123");
string temp2 = new string("123");

This would cause 2 strings to be generated and the strings would not be
put in the pool.

Happy coding!
Morten Wennevik [C# MVP]


Eddie Pazz< dr **** @ hotmail.com>写道:
Eddie Pazz <dr****@hotmail.com> wrote:
虽然读到C#中的所有内容都是一个对象,但为什么我能这样做:

string temp ="" ;;

而不是

字符串temp = new string("");

这只是编译器提供的快捷方式吗?
While reading that everything in C# is an object, why is it that I can do:

string temp = "";

instead of

string temp = new string("");

Is this just a shortcut the compiler provides?




你认为第二个会调用什么构造函数?


代码中存在的字符串文字确保带有

的字符串适当的数据在运行时可用。字符串被实习,

所以只有一个对象为所有具有相同数据的文字创建。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



What constructor would you believe the second would call?

A string literal being present in code makes sure that a string with
the appropriate data in is available at run time. Strings are interned,
so that only one object is created for all literals with the same data.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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

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