本文介绍了get的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! Ms定义如下:属性或索引器中的访问器方法 检索属性或索引器元素的值。 所以是以下的TestDirectory得到了不好的做法? 类框架 { 字符串目录; static string testFolder =" Test"; 公共静态字符串TestFolder {get {return testFolder; } $ 公共静态字符串TestDirectory {get {return directory + @" \" + testFolder; } $ 公共框架(字符串目录) { this.directory = directory; } } 我应该这样做: 公共静态字符串GetTestDirectory() { 返回目录+ @" \" + testFolder; } 谢谢, AineMs defines get as follows: an accessor method in a property or indexerthat 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推荐答案 < snip> 看起来很好,虽然TestDirectory和TestFolder不是很好 清楚地区分了 - 我不知道会有什么期望差异 只是基于名字。 Jon<snip>Seems fine to me, although TestDirectory and TestFolder aren''t veryclearly distinguished - I wouldn''t know what to expect the differenceto be just based on the name.Jon 您好Aine, 我认为它把它变成财产真好。一个小的改变是我可能会使用Path.Combine()来连接目录和 testFolder(而不是+ @\++)将处理 目录有一个尾部反斜杠的情况。我也可能会将 结果保存在一个成员varialbe中,这样每次有人调用Framework.TestDirectory时你都不会把路径结合起来。 。 JohnHi Aine,I think it''s fine to make it a property. One minor change is that Iwould probably use Path.Combine( ) to concatenate directory andtestFolder (instead of +@"\"+), which will handle cases wheredirectory has a trailing backslash. I would also probably save theresult in a member varialbe so you don''t end up combining the pathevery time someone calls Framework.TestDirectory.John < snip> 看起来很好,虽然TestDirectory和TestFolder不是很好 清楚地区分 - 我不知道会有什么期望差异 只是基于名称。 Jon<snip>Seems fine to me, although TestDirectory and TestFolder aren''t veryclearly distinguished - I wouldn''t know what to expect the differenceto 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 adirectory or file, a directory is: C:/folder1/folder2/folder3, and afolder is folder1, folder2 or folder3. 这篇关于get的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-01 01:56