为什么字符串类没有构造函数取0参数 [英] why string class have not constructor take 0 arguments

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

问题描述

为什么字符串类没有构造函数取0参数?

为什么我不能写这个

string text = new string();

为什么大多数都要给出参数

解决方案

如果你想要一个空字符串,你可以简单地声明变量。

  string  txt; 



如果您希望字符串在构造时包含数据,则必须告诉你想要的构造函数。因此,您必须为构造函数提供参数。



您当然也可以指定一个字符串常量来初始化字符串。

  string  txt =   Hello World ; 







你要做的是什么,你不要不想为构造函数提供参数?


ebousha问:

我想知道为什么classstring没有默认构造函数,它接受0个参数? [语法改进:-) - SA]

出于好奇,这是一个合理甚至有趣的问题,但不是那个需要理性答案的问题。缺乏这样的构造函数以及它的存在并不违反自然法则,.NET设计或良好的软件实践。唯一真正正确的答案是:因为FCL的创造者决定这样做。他们可以轻松地使这个构造函数可用并等效于指定 string.Empty 的null。这种决定的明显原因是:在这样的构造函数中绝对没有实际需要。任何人都可以指定null或 string.Empty ;并且,作为明确的作业,它不会留下任何疑问



(只有一句话:null vs空字符串有一个程序员之间相当蹩脚的争论的悠久历史,我认为使用两者都具有相当大的价值;并且明确区别是缓解那些蹩脚争议的一种方式。:-))



一些背景知识:您应该知道的一件事:如果您没有定义构造函数,则会隐式定义无参数构造函数,以方便开发人员。如果添加任何带有任何参数的构造函数,则它为undefined隐式无参数构造函数。 System.String 就是这种情况。要再次定义无参数构造函数,您需要明确定义。







当然,Dave(见解决方案3)是对的。如果string是可变的,那么这个构造函数会有所帮助。使用不可变的字符串,你可以反复构造字符串...



-SA


< blockquote>没有无参数构造函数,因为在.NET中,一旦创建,就无法更改String。因此,如果无法更改String,那么无参数构造函数的意义何在?你可以用这样的构造函数做的唯一事情是创建一个空的String对象(0长度),这与说:

 string myString = String.Empty; 


why string class have not constructor take 0 arguments ??
why i can not write this
string text = new string();
why most be give arguments

解决方案

If you want a null string you can simply declare the variable.

string txt;


If you want the string to contain data when you construct it you must tell the constructor what you want. Therefore you must supply parameters to the constructor.

You can, of course also assign a string constant to initialize a string.

string txt = "Hello World";




What is it that you are trying to do that you don''t want to supply parameters to the constructor?


ebousha asked:

I want to know why the class "string" has no default constructor which takes 0 arguments? [grammar improved :-) — SA]

Out of curiosity, this is a reasonable and even interesting question, but not the one which can require a rational answer. The lack of such constructor, as well as its presence, does no violate laws of nature, .NET design or good software practices. The only really correct answer would be: "because the creators of the FCL decided to do so". They could easily make this constructor available and equivalent to assigning null of string.Empty. And pretty obvious reason of such decision is this: there is absolutely no practical need in such constructor. Anyone can assign null or string.Empty; and, as the explicit assignment, it won''t leave any doubt.

(Just one remark: null vs empty strings has a long history of rather lame arguments between programmers, and I see a considerable value in using both; and making explicit difference is a way of alleviation of those lame controversies. :-))

Some background: one thing you should know: if you define no constructors, a parameterless constructor is implicitly defined, for developer''s convenience. If you add any constructor with any parameters, it "undefined" implicit parameterless constructor. This is the case with System.String. To make a parameterless constructor defined again, you would need to do it explicitly.

[EDIT]

Of course, Dave (see Solution 3) is right. If string was mutable, this constructor would make some sense. With non-mutable strings, you construct the string over and over...

—SA


There is no parameterless constructor because, in .NET, once created, a String cannot be changed. So if the String cannot be changed, what''s the point of a parameterless constructor? The only thing you can do with such a constructor is create an empty String object (0 length), which is identical to saying:

string myString = String.Empty;


这篇关于为什么字符串类没有构造函数取0参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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