【发布时间】:2020-07-06 23:28:26
【问题描述】:
在this 旧 Microsoft 文档 Richard Siddaway 中声明
我使用让人联想到属性名称的变量名称。这些 变量是任意的,你可以使用任何合法的变量名—— 避免使用属性的名称!
这导致构造函数看起来像这样
RStimezone(
[int]$idx,
[string]$desc,
[string]$tmzn,
[bool]$crnt
){
$this.Index = $idx
$this.Description = $desc
$this.Timezone = $tmzn
$this.Current = $crnt
}
但没有解释为什么建议使用这种命名约定。据我所知,没有范围问题,因此使用与类属性同名的参数不会引起问题。在我看来,这是一种更具可读性的方法。
RStimezone(
[int]$index,
[string]$description,
[string]$timezone,
[bool]$current
){
$this.Index = $index
$this.Description = $description
$this.Timezone = $timezone
$this.Current = $current
}
我错过了什么吗?
【问题讨论】:
-
我也会选择更具描述性/可读性的变量名称。只要您不(误)使用Automatic variable 名称,应该没有问题。
-
@Theo,我有点想知道这是否基本上是 C# 的最佳实践或成语,类似于它们自己的行上的花括号。很高兴知道我终于对 PowerShell 有了一些直觉。 :)
标签: powershell class properties arguments