疯狂的想法?命令参数在构造函数中 [英] crazy idea?? named parameters in constructor

查看:62
本文介绍了疯狂的想法?命令参数在构造函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为能够像
这样的代码真的很好

someVariable = someMethod(new SomeClass(some text));


但是,由于方法签名是参数的数量和类型,例如,你只需要有一个接受单个字符串的构造函数。


叫我疯了,但是能够有一个语法

你可以在如上所述的语句中设置属性并不是很有用br />
仅限于您可以合并到ctor签名中的内容吗?


公共类SomeClass

{

private string _var1;

private string _var2;

private string _var3;


//公共属性Var1,Var2和Var3这里


public SomeClass()

{

}

}


然后可以说


someVariable = someMethod(new SomeClass(!V​​ar2 =" some) text,

!Var3 =" something else));


!当然是我刚刚编造的一些惯例。关键是

它仍然允许上面相当简洁的线与

之后的比较:

SomeClass cls = new SomeClass();

cls.Var2 =" some text";

cls.Var3 =" something else";

someVariable = someMethod(cls);


但是它在使用

someMethod()之前提供了完全灵活性。 (想想使用的方式看起来比try.finally

(并测试为null)块更清晰。

I think it''s really nice to be able to do code like

someVariable = someMethod(new SomeClass("some text"));

However, as method signatures are number and types of parameters, you''re
limited to having one constructor that accepts a single string, for example.

Call me crazy, but wouldn''t it be useful to be able to have a syntax where
you could set properties in a statement like the one above, without being
limited to just what you could incorporate into a ctor signature?

public class SomeClass
{
private string _var1;
private string _var2;
private string _var3;

// public properties Var1, Var2, and Var3 here

public SomeClass()
{
}
}

and then be able to say

someVariable = someMethod(new SomeClass(!Var2 = "some text",
!Var3="something else"));

The "!" being of course some convention I just made up. The point being
that it still allows the rather concise line above compared to the
following:
SomeClass cls = new SomeClass();
cls.Var2 = "some text";
cls.Var3 = "something else";
someVariable = someMethod(cls);

But it gives total flexibility on what''s set before it is used in
someMethod(). (Think of the way "using" looks cleaner than the try..finally
(and test for null) blocks.

推荐答案



抱歉,但不能说我同意。你的提议看起来很混乱。我个人更喜欢多行清晰。我们最不需要的是将越来越多的代码压缩到一行


----- Daniel Billingsley写道:----


我认为能够很好做代码怎么样


someVariable = someMethod(new SomeClass(" some text"))


但是,因为方法签名是数字和参数的类型,你只需要一个接受一个字符串的构造函数,例如


叫我疯了,但不会能够有一个语法是有用的

你可以在如上所述的语句中设置属性,而不是仅仅b
$ b仅限于你可以合并的内容成为一个ctor签名


公共类SomeClas


私有字符串_var1

私有字符串_var2

private string _var3


//公共属性Var1,Var2和Var3她


public SomeClass(



然后能够sa />

someVariable = someMethod(new SomeClass(!V​​ar2 =" some text"

!Var3 =其他东西))


!当然是我刚刚编造的一些惯例。关于

之后,它仍然允许上面相当简洁的一行

SomeClass cls = new SomeClass()

cls.Var2 =" some text"

cls.Var3 =" something else"

someVariable = someMethod(cls)


但它在使用之前提供了完全的灵活性。

someMethod()。 (想想使用的方式看起来比try ... finall更清洁

(并测试为null)块


sorry but can''t say that I agree. your proposal looks very messy. I personally prefer multiple lines for clarity. the last thing we need is to squeeze more and more code into one line

----- Daniel Billingsley wrote: ----

I think it''s really nice to be able to do code lik

someVariable = someMethod(new SomeClass("some text"))

However, as method signatures are number and types of parameters, you''r
limited to having one constructor that accepts a single string, for example

Call me crazy, but wouldn''t it be useful to be able to have a syntax wher
you could set properties in a statement like the one above, without bein
limited to just what you could incorporate into a ctor signature

public class SomeClas

private string _var1
private string _var2
private string _var3

// public properties Var1, Var2, and Var3 her

public SomeClass(


and then be able to sa

someVariable = someMethod(new SomeClass(!Var2 = "some text"
!Var3="something else"))

The "!" being of course some convention I just made up. The point bein
that it still allows the rather concise line above compared to th
following
SomeClass cls = new SomeClass()
cls.Var2 = "some text"
cls.Var3 = "something else"
someVariable = someMethod(cls)

But it gives total flexibility on what''s set before it is used i
someMethod(). (Think of the way "using" looks cleaner than the try..finall
(and test for null) blocks


>但它在
someMethod()中使用它之前设置的内容具有完全的灵活性。(想想使用的方式看起来比
尝试更清晰。 .finally(和测试为null)块。
someMethod(). (Think of the way "using" looks cleaner than the try..finally (and test for null) blocks.




有些语言支持这种语法,但在C#中你可以随时提供
在构造函数上有多个重载。这并没有实现你正在问的确切但是是一种广泛使用的替代方法。


语法C#中的属性也支持,只要您使用get \set访问者提供

公共属性,您就可以执行以下操作:


[MyAttribute( Var2 =some text,Var3 =其他东西)]


但是,再一次,并不是你的建议g(特定于

属性)。我不认为你会在所有课程中看到这一点,因为多个构造函数重载多次执行同样的事情(以更有限的方式)。


n!



There are languages where this syntax is supported, but in C# you can always
provide multiple overloads on the constructor. This doesn''t achieve what
you''re asking exactly but is an alternative that is widely used.

The syntax is also supported by attributes in C#, as long as you provide
public properties with get\set accessors you can do the following:

[MyAttribute( Var2="some text", Var3="something else" )]

But again, isn''t exactly what you''re suggesting (being specific to
attributes). I don''t think you''ll see this in all classes, as multiple
constructor overloads do the same thing (in a more constrained fashion).

n!


我很困惑。为什么不这样做:

公共类SomeClass

{

private s1 = null;

private s2 = null;

private s3 = null;

public SomeClass()

{

}

public SomeClass(s​​tring s1,string s2,string s3)

{

this.s1 = s1;

this.s2 = s2;

this.s3 = s3

}

}


测试空值和值适合你的班级等。


-

William Stacey,MVP


丹尼尔比林斯利" <分贝********** @ NO.durcon.SPAAMM.com>在消息中写道

新闻:#k ************** @ TK2MSFTNGP09.phx.gbl ...
I am confused. Why not just do this:
public class SomeClass
{
private s1 = null;
private s2 = null;
private s3 = null;
public SomeClass()
{
}
public SomeClass(string s1, string s2, string s3)
{
this.s1 = s1;
this.s2 = s2;
this.s3 = s3
}
}

Test for nulls and values as is right for your class, etc.

--
William Stacey, MVP

"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> wrote in message
news:#k**************@TK2MSFTNGP09.phx.gbl...
我认为它'很高兴能够做类似的代码

someVariable = someMethod(new SomeClass(some some));

但是,因为方法签名是数字和参数的类型,你只能使用一个接受单个字符串的构造函数,例如

叫我疯了,但能做到没有用有一个语法,你可以在如上所述的语句中设置属性,而不仅限于你可以合并到ctor签名中的内容吗?

公共类SomeClass <私有字符串_var1;
私有字符串_var2;
私有字符串_var3;

//公共属性Var1,Var2和Var3这里

公共SomeClass()
{
}
}
然后可以说

someVariable = someMethod(新索姆eClass(!V​​ar2 =" some text",
!Var3 =" something else));

!当然是我刚刚编造的一些惯例。关键是它仍然允许上面相当简洁的线与下面的
相比:
SomeClass cls = new SomeClass();
cls.Var2 =" some text" ;
cls.Var3 =其他东西;
someVariable = someMethod(cls);

但它在使用之前提供了完全灵活的设置
someMethod()。 (想想使用的方式看起来比
try..finally(和测试为null)块更清晰。
I think it''s really nice to be able to do code like

someVariable = someMethod(new SomeClass("some text"));

However, as method signatures are number and types of parameters, you''re
limited to having one constructor that accepts a single string, for example.
Call me crazy, but wouldn''t it be useful to be able to have a syntax where
you could set properties in a statement like the one above, without being
limited to just what you could incorporate into a ctor signature?

public class SomeClass
{
private string _var1;
private string _var2;
private string _var3;

// public properties Var1, Var2, and Var3 here

public SomeClass()
{
}
}

and then be able to say

someVariable = someMethod(new SomeClass(!Var2 = "some text",
!Var3="something else"));

The "!" being of course some convention I just made up. The point being
that it still allows the rather concise line above compared to the
following:
SomeClass cls = new SomeClass();
cls.Var2 = "some text";
cls.Var3 = "something else";
someVariable = someMethod(cls);

But it gives total flexibility on what''s set before it is used in
someMethod(). (Think of the way "using" looks cleaner than the try..finally (and test for null) blocks.






这篇关于疯狂的想法?命令参数在构造函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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