get的最佳实践 [英] Best practice with get
问题描述
Ms定义如下:属性或索引器中的访问器方法
检索属性或索引器元素的值。
所以是以下的TestDirectory得到了不好的做法?
类框架
{
字符串目录;
static string testFolder =" Test";
公共静态字符串TestFolder {get {return testFolder; } $
公共静态字符串TestDirectory {get {return directory
+ @" \" + testFolder; } $
公共框架(字符串目录)
{
this.directory = directory;
}
}
我应该这样做:
公共静态字符串GetTestDirectory()
{
返回目录+ @" \" + testFolder;
}
谢谢,
Aine
Ms defines get as follows: an accessor method in a property or indexer
that retrieves the value of the property or the indexer element.
So is the following TestDirectory get bad practice?
class Framework
{
string directory;
static string testFolder = "Test";
public static string TestFolder { get { return testFolder; }}
public static string TestDirectory{ get { return directory
+@"\"+testFolder; }}
public Framework(string directory)
{
this.directory = directory;
}
}
should I do this instead:
public static string GetTestDirectory()
{
return directory+@"\"+testFolder;
}
Thanks,
Aine
推荐答案
10月24日下午1:12,aine_ca ... @ yahoo.com写道:
On Oct 24, 1:12 pm, aine_ca...@yahoo.com wrote:
Ms定义如下:属性或索引器中的访问器方法
检索值的属性或索引器元素。
以下TestDirectory是不是很糟糕?
Ms defines get as follows: an accessor method in a property or indexer
that retrieves the value of the property or the indexer element.
So is the following TestDirectory get bad practice?
< snip>
看起来很好,虽然TestDirectory和TestFolder不是很好
清楚地区分了 - 我不知道会有什么期望差异
只是基于名字。
Jon
<snip>
Seems fine to me, although TestDirectory and TestFolder aren''t very
clearly distinguished - I wouldn''t know what to expect the difference
to be just based on the name.
Jon
10月24日上午8:12,aine_ca ... @ yahoo.com写道:
On Oct 24, 8:12 am, aine_ca...@yahoo.com wrote:
Ms定义如下:属性或索引器中的访问器方法
检索属性或索引器元素的值。
所以是以下TestDirectory得到了不好的做法?
类框架
{
字符串目录;
static string testFolder =" Test";
公共静态字符串TestFolder {get {return testFolder; } $
公共静态字符串TestDirectory {get {return directory
+ @" \" + testFolder; } $
公共框架(字符串目录)
{
this.directory = directory;
}
}
我应该这样做:
公共静态字符串GetTestDirectory( )
{
返回目录+ @" \" + testFolder;
}
>
谢谢,
Aine
Ms defines get as follows: an accessor method in a property or indexer
that retrieves the value of the property or the indexer element.
So is the following TestDirectory get bad practice?
class Framework
{
string directory;
static string testFolder = "Test";
public static string TestFolder { get { return testFolder; }}
public static string TestDirectory{ get { return directory
+@"\"+testFolder; }}
public Framework(string directory)
{
this.directory = directory;
}
}
should I do this instead:
public static string GetTestDirectory()
{
return directory+@"\"+testFolder;
}
Thanks,
Aine
您好Aine,
我认为它把它变成财产真好。一个小的改变是我可能会使用Path.Combine()来连接目录和
testFolder(而不是+ @\++)将处理
目录有一个尾部反斜杠的情况。我也可能会将
结果保存在一个成员varialbe中,这样每次有人调用Framework.TestDirectory时你都不会把路径结合起来。
。
John
Hi Aine,
I think it''s fine to make it a property. One minor change is that I
would probably use Path.Combine( ) to concatenate directory and
testFolder (instead of +@"\"+), which will handle cases where
directory has a trailing backslash. I would also probably save the
result in a member varialbe so you don''t end up combining the path
every time someone calls Framework.TestDirectory.
John
24 Okt,14:17,Jon Skeet [C#MVP]" < sk ... @ pobox.comwrote:
On 24 Okt, 14:17, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
10月24日下午1点12分,aine_ca ... @ yahoo.com写道:
On Oct 24, 1:12 pm, aine_ca...@yahoo.com wrote:
Ms定义如下:属性或索引器中的访问器方法
检索属性或索引器元素的值。
Ms defines get as follows: an accessor method in a property or indexer
that retrieves the value of the property or the indexer element.
以下TestDirectory是不是很糟糕?
So is the following TestDirectory get bad practice?
< snip>
看起来很好,虽然TestDirectory和TestFolder不是很好
清楚地区分 - 我不知道会有什么期望差异
只是基于名称。
Jon
<snip>
Seems fine to me, although TestDirectory and TestFolder aren''t very
clearly distinguished - I wouldn''t know what to expect the difference
to be just based on the name.
Jon
..
是的,我同意,但评论会清楚这一点。对我来说,路径是一个
目录或文件,目录是:C:/ folder1 / folder2 / folder3,
文件夹是folder1,folder2或folder3。
..
Yeah, I agree but the comments will clear that up. For me, a path is a
directory or file, a directory is: C:/folder1/folder2/folder3, and a
folder is folder1, folder2 or folder3.
这篇关于get的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!