这是好用的属性吗? [英] Is this good use of Properties?

查看:71
本文介绍了这是好用的属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我这样做而没有声明相应的字段,是否认为不好

设计?这两种方法有哪些优点或缺点?注意

没有设置。


公共字符串URL

{

get

{

return" www.somewhere.com/test.aspx" ;;

}

}


与更常见的方法相比:

private readonly string _URL =" www.somewhere.com/test.aspx" ;;


公共字符串URL

{

get

{

返回_URL;

}

}


另外,如果我只有一个get访问器,是否需要将该字段声明为

readonly?


谢谢,

Brett

If I do this without declaring a corresponding field, is it considered bad
design? What are the advantages or disadvantages to either method? Notice
there is not set.

public string URL
{
get
{
return "www.somewhere.com/test.aspx";
}
}

vs. a more common approach:
private readonly string _URL = "www.somewhere.com/test.aspx";

public string URL
{
get
{
return _URL;
}
}

Also, if I only have a get accessor, is it necessary to declare the field as
readonly?

Thanks,
Brett

推荐答案

如果我这样做而没有声明相应的字段,那么它被认为是不好的设计吗?


不在我看来。


这两种方法有哪些优点或缺点?


如果你添加一个实例字段来存储一个常量值,你就会浪费一些内存来创建该类的每个对象。除非你有很多这样的对象,否则这可能不是一个很大的优惠,但是仍然可以使它变得更有意义(静态)常量或只是放置

属性getter中的字符串内联。


另外,如果我只有一个get访问器,是否有必要将该字段声明为
readonly?
If I do this without declaring a corresponding field, is it considered bad
design?
Not in my opinion.

What are the advantages or disadvantages to either method?
If you add an instance field to store a constant value you''re wasting
some memory for every object created of that class. That may not be a
big deal unless you''re going to have lots of such objects, but still
it would make more sense to make it a (static) constant or just place
the string inline in the property getter.

Also, if I only have a get accessor, is it necessary to declare the field as
readonly?







Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。



No

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


" Brett" < no@spam.com>写在

新闻:#C ************** @ tk2msftngp13.phx.gbl:
"Brett" <no@spam.com> wrote in
news:#C**************@tk2msftngp13.phx.gbl:
如果我这样做这没有声明相应的字段,是否认为设计不好?
方法有哪些优点或缺点?请注意,没有设置。
If I do this without declaring a corresponding field, is it considered
bad design? What are the advantages or disadvantages to either
method? Notice there is not set.




当然,我一直这样做。如果你只是返回一个恒定值

那么你可以像公开读取一样容易地公开它,但是这样做是没有错的。事实上,如果您希望在设计师中显示一个值但不希望用户能够改变价值,那么有时会使用这个值。<\\ n>
br />

如果你不想要,你不必声明一个只能由get-only
访问者控制的字段,事实上可能不想如果

代码的其他部分改变了价值。


-mdb



Sure I do that all the time. If you are only returning a constant value
then you could just as easily expose it as a public readonly, but there''s
nothing wrong with doing it this way. In fact, this is used sometimes if
you want to show a value in the designer but don''t want the user to be able
to change the value.

You do not have to declare a field that is controlled by a get-only
accessor as readonly if you don''t want to, and in fact may not want to if
other parts of the code change the value.

-mdb


与脑死亡的VB编译器不同,C#编译器可以找出

本身,如果一个属性只有一个get访问器,那么它可能是一个

只读属性。这是我的VB宠物讨厌之一。


也许您提到的财产类型会更好,因为静态

属性类似于Pi数学类或红色在Color类中。


-

Bob Powell [MVP]

Visual C#,System.Drawing


在Windows窗体中查找优秀的Windows窗体文章提示和技巧
http://www.bobpowell.net/tipstricks.htm

用GDI +常见问题回答那些GDI +问题
http://www.bobpowell.net/faqmain.htm


所有新文章都提供C#和VB.NET中的代码。

订阅所提供的RSS提要,绝不会错过任何新文章。


"布雷特" < no@spam.com>在消息中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
The C# compiler, unlike the brain-dead VB compiler, can figure out for
itself that if a property only has a get accessor then it''s probably a
read-only property. It''s one of my VB pet hates.

Perhaps the type of property you mention would be better as a static
property similar to Pi in the Math class or "Red" in the Color class.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Brett" <no@spam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
如果我这样做而不声明相应的字段,它被认为是不好的设计吗?这两种方法有哪些优点或缺点?
注意没有设置。

公共字符串URL
{
获取
{
返回" www.somewhere.com/test.aspx" ;;
}

与更常见的方法相比:

private readonly string _URL =" www.somewhere.com/test.aspx" ;;

公共字符串网址
{
获取
{
返回_URL;
谢谢,
Brett
If I do this without declaring a corresponding field, is it considered bad
design? What are the advantages or disadvantages to either method?
Notice there is not set.

public string URL
{
get
{
return "www.somewhere.com/test.aspx";
}
}

vs. a more common approach:
private readonly string _URL = "www.somewhere.com/test.aspx";

public string URL
{
get
{
return _URL;
}
}

Also, if I only have a get accessor, is it necessary to declare the field
as readonly?

Thanks,
Brett



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

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