函数应该以“Get”开头吗?

发布于 2024-08-18 20:17:59 字数 158 浏览 2 评论 0原文

我正在尝试查看 C# 编码标准,以使我的代码更加美观和标准,我有一个问题:根据 C# 编码标准,函数(不是 void 的方法)是否应该以“Get”开头?例如:“GetSongOrder()”、“GetNextLine()”等?

谢谢。

I'm trying to look at the C# Coding Standards to make my code more beautiful and standard, and I have a question: Does functions (methods that are not voids), according to the C# coding standards, should start with "Get"? For example: "GetSongOrder()", "GetNextLine()" etc?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

有两种情况:

如果例程可以非常快速地“获取”值,而不会在代码中造成副作用,我建议使用属性 getter,并省略“获取”名称:

public SongOrder SongOrder
{
   get { return this.songOrder; } // ie: an enum
}

但是,如果需要处理,则名称为“Get...”的方法是合适的。这表明调用此方法可能会产生副作用,例如额外的处理或某些状态的更改:

public string GetNextLine()
{
    string line = this.stream.ReadLine(); // This may cause a longer running operation, especially if it's using Disk IO/etc
    // do other work?
    return line;
}

There are two cases:

If the routine can "get" the value very quickly, without causing side effects in the code, I recommend using a property getter, and omitting the "get" name:

public SongOrder SongOrder
{
   get { return this.songOrder; } // ie: an enum
}

If, however, there is processing required, then a method with the "Get..." name is appropriate. This suggests that there may be side effects to calling this method, such as extra processing, or a change in some state:

public string GetNextLine()
{
    string line = this.stream.ReadLine(); // This may cause a longer running operation, especially if it's using Disk IO/etc
    // do other work?
    return line;
}
各空 2024-08-25 20:17:59

您的函数名称应该简洁且有意义。不多不少。

如果 get 适合您,则使用 Get。如果检索有效,请使用它。

命名约定没有真正的“标准”。

Your function name should be succinct, and make sense. Nothing more, nothing less.

If get works for you, then use Get. If Retrieve works, use that.

There is no true "standard" for naming conventions.

幽梦紫曦~ 2024-08-25 20:17:59

一般来说,函数的动词-名词命名约定很常见。 “获取”只是您可能看到的动词之一。它也可以是“激活”、“关闭”、“转储”或任何东西......这只是英语和一般编码的结果,方法或函数通常会对“某种类型的对象”进行“做某事” '。

In general, a Verb-Noun naming convention for Functions is common. 'Get' is just one of the Verbs you might see. It could also be 'Activate', 'Close', 'Dump', or anything... it's just a consequence of English and general coding that a method or function will typically 'Do Something' to 'Some Type of Object(s)'.

ぃ弥猫深巷。 2024-08-25 20:17:59

我还没有看到这方面的 C# 标准,但我见过的大多数实际代码都使用“getter”属性,并且它们在函数名称中省略了“Get”一词。
示例:

Public SongOrderList SongOrder
{
    get
    {
      return mySongOrderList;
    }
}

使用“Get”(和“Set”)作为函数名称前缀是我在不具有 .NET 属性的语言(例如 C、Java 等语言)中经常看到的情况。

编辑:

...当然你也可以有setter

Public SongOrderList SongOrder
{
    get
    {
      //do some processing code here
      return mySongOrderList;
    }
    set
    {
      //do some processing code here
      mySongOrderList = value; //value is a C# keyword, in case you didn't know. it is the parameter to the setter
    }
}

当然,如果你想要一个getter和一个setter并且你不需要任何额外的处理,只需要纯Java-bean-like get/设置 youc 并执行此操作:

Public SongOrderList SongOrder
    {
        get; set;
    }

如果您只想要一个 PUBLIC getter,则可以执行此操作(我认为 - 已经有一段时间了):

Public SongOrderList SongOrder
    {
        public get; private set;
    }

I haven't seen a C# standard on that, but most of the actual code I've seen written uses "getter" properties and they omit the word "Get" from the function name.
Example:

Public SongOrderList SongOrder
{
    get
    {
      return mySongOrderList;
    }
}

Using "Get" (and "Set") as a function name prefix is something I usually see in langauges that don't have properties a la .NET (languages such as C, Java).

Edit:

...and of course you can have setters too

Public SongOrderList SongOrder
{
    get
    {
      //do some processing code here
      return mySongOrderList;
    }
    set
    {
      //do some processing code here
      mySongOrderList = value; //value is a C# keyword, in case you didn't know. it is the parameter to the setter
    }
}

Of course, if you want a getter and a setter and you don't need any extra processing, just pure Java-bean-like get/set youc an do this:

Public SongOrderList SongOrder
    {
        get; set;
    }

If you only want a PUBLIC getter you can do this (I think - it's been a while):

Public SongOrderList SongOrder
    {
        public get; private set;
    }
梦言归人 2024-08-25 20:17:59

不,没有标准规定函数应该以“Get”开头。

当创建有意义的函数名称时,它们通常采用这种形式,例如 GetHashCode 函数。开头的其他动词很常见,例如在 ToString 函数中,并且只有像在 Encode 函数中的动词。

还有其他形式,您可以在其中描述函数的结果,例如 Substring 函数。

No, there is no standard that function should start with "Get".

When creating function names that make sense, they often take that form, like the GetHashCode function. Other verbs in the beginning are common, like in the ToString function, and only verbs like in the Encode function.

There are other forms, where you rather describe the result of the function, like the Substring function.

德意的啸 2024-08-25 20:17:59

除了其他答案 - 如果您有像 GetNextLine() 这样的方法,您可能需要考虑返回枚举,例如

IEnumerable; GetLines();

In addition to other answers - if you have a method like GetNextLine(), you may want to consider returning an enumeration instead e.g.

IEnumerable<Line> GetLines();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文