字符串类问题 [英] String Class Question

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

问题描述





有什么区别



String s;



String s = new String(new char [] {'H'});



What is the difference between

String s;

String s = new String(new char[] {'H'});

推荐答案

忽略它不会编译,因为没有没有参数的String构造函数...



Ignoring that it won't compile, because there is no String constructor which takes no parameters...

String s;

声明一个变量 可以 保存对字符串的引用实例。

Declares a variable which can hold a reference to a string instance.

String s = new String();

声明变量并为变量分配一个空字符串实例。



它有点像水玻璃:第一个版本制作玻璃,第二个版本填充液体。如果您只是申报玻璃,则不能对其进行任何操作,例如饮用液体,或搅拌液体,或冷冻液体。

你必须先将(填充)字符串(水)实例分配给变量(玻璃),然后再用它做任何事情。







我的问题是:

如果我可以操纵两个字符串值那么两种类型的声明之间有什么区别.string s;和string s = new string(new char [] {'h'});

我可以使用字符串s来使用所有的字符串函数;只有这样才是什么意思


Declares the variable and assigns an empty string instance to the variable.

It's a bit like a water glass: the first version creates the glass, and the second fills it with a liquid. If you only declare the glass, you can't do anything with it, like drink the liquid, or stir the liquid, or freeze the liquid.
You have to assign (fill) the string (water) instance to the variable (glass) before you can do anything with it.



"My question is:
If i can manipulate both the string values then what's the difference between both types of declaration. i.e. string s; and string s=new string (new char[] {'h'});
I can use all the string function by using string s; only then what's the meaning of

string s=new string(new char[] {'h'});



< b>两者都是一样的。

我要问区别。





否,你不能。

如果你使用


both are same.
I am asking the difference."



No, you can't.
If you use

String s;

然后使用 s 上的任何字符串函数将

a)不编译,因为你会得到一个使用未分配的局部变量's'错误,



b)抛出异常:对象引用未设置为对象的实例。

这两个都是 s 能力 持有一个字符串实例,<我>但实际上并没有这样做。



当你使用任何表格时:

then using any of the string functions on s will
a) Not compile because you will get a "Use of unassigned local variable 's'" error,
and
b) Throw an exception: "Object reference not set to an instance of an object."
Both of these are down to s being capable of holding an instance of a string, but not actually doing so.

When you use any of the forms:

string s = ...

例如

such as

string s = "My String";
string s = new string(new char[] {'M', 'y', ' ', 'S', 't', 'r', 'i', 'n', 'g'});
string s = new string('X', 6);
string s = new string(new char[] {'M', 'y', ' ', 'S', 't', 'r', 'i', 'n', 'g'}, 3, 5);



然后你要为变量赋值,然后给它对字符串实例的引用。



为变量赋值的各种方法只为你提供了不同的选项来做同样的事情:


Then you are assigning a value to the variable, and giving it a reference to a string instance.

The various ways of assigning a value to the variable just give you different options for doing the same thing:

string s = "h";
string s = new string(new char[] {'h'});
string s = new string('h', 1);



最终会得到同样的结果: s 包含对包含单个字符串实例的字符串实例的引用字符:'h'



不同的选项只是让你按照代码需要的方式做事:因为字符串实际上只是一个 char 值的数组。



事实上,完全合法的说法是:


will all end up with the same thing: s containing a reference to a string instance that contains a single character: 'h'

The different options just let you do things the way your code needs: because a string is effectively just an array of char values.

In fact it is perfectly legal to say:

for (int i = 0; i < s.Length; i++)
    {
    Console.WriteLine(s[i]);
    }



Or

foreach (char c in s)
    {
    Console.WriteLine(c);
    }

你唯一不能做的是:

The only thing you can't do is:

s[4] = '


';
';

因为字符串是不可变的,这意味着一旦创建,你就无法以任何方式改变它 - 只需制作一份与众不同的副本。 (目前可以忽略这一点,但后来变得很重要)

because strings are immutable which means that once created, you cannot change it in any way - just make a copy with a difference in it. (This can be ignored at the moment, but it becomes significant later)


我想知道你问题的原因是什么。

如果你试试看的话编译和执行,你会看到症状的不同。



例如



I wonder what is the cause of your question.
If you try it out by compiling and executing, you'll see the difference of the symptoms.

E.g.

static void Main(string[] args)
{
    {
        string s;
        Console.WriteLine(s); // <--- compilation error: "Use of unassigned local variable 's'"
    }
    {
        string s = new string(new char[] {'H'});
        Console.WriteLine(s);
    }
}





编译器告诉你有什么区别:一个之前没有分配任何值使用它。





string 是一个引用类型价值语义。如果在类中使用未显式初始化字段,则隐式初始值(引用类型)为 null 。相反:如果在某些代码块中用作 local 变量,编译器会抱怨如果没有初始化,也没有为字符串赋值。



例如这个编译没有错误(相对于上面没有编译的代码示例):



The compiler tells you "what's the difference": one is not assigned any value before it is used.


string is a reference type with value semantics. If used as not explicitly initialized field in a class, the implicit initial value (of a reference type) is null. In contrast: if used as local variable in some code block, the compiler complains if not initialized nor assigned some value to the string.

E.g. this compiles without error (versus the code sample above that does not compile):

class MyClass
{
    public string _s;
}

static void Main(string[] args)
{
    {
        MyClass a = new MyClass();
        Console.WriteLine(a._s == null); // Output: True
    }
}



[/ EDIT1]



干杯

Andi


[/EDIT1]

Cheers
Andi


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

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