在 C# 中将参数作为最终参数传递
这可能是一个重复的问题。但在搜索中找不到它 在java中,将方法参数标记为常量,我们将其声明为final,等效的C#关键字是什么? 喜欢
public void doSomeThing(final object myObject)
{
//print myobject
}
This might be a duplicate question.But could not find it in search
In java to mark a method parameter as constant we declare it as final whats the equivalent C# keyword?
Like
public void doSomeThing(final object myObject)
{
//print myobject
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这在 C# 中是不可能的 - 无法将传入的参数标记为常量。
如果您有一个需要对许多函数可用的 const,为什么不使用正确的作用域(类作用域或全局,如果需要的话)来声明它呢?
This is not possible in C# - there is no way to mark a passed in parameter as a constant.
If you have a
const
that needs to be available to many functions, why not declare it with the right scoping (class scoping or global if needed)?C# 中有一个 readonly 关键字,但到目前为止我们只能将其应用于字段,而不能应用于局部变量或参数。
在 Java 中,
final
关键字通常作为一种风格而被习惯性地使用,以便更容易地关闭匿名内部类中的值。但这个问题在 C# 中不存在,因为匿名方法/lambda 可以关闭可修改的变量。此外,像 Resharper 这样的工具可以用不同的颜色显示变异的变量/参数,这样您就可以一目了然地看到方法中的“复杂情况”。我用它来使突变的名字以粗体绿色文本或类似的文字亮起!
这并不是说 C# 本身不会通过更多功能来改进,例如广泛适用的
readonly
来帮助声明不变性。但是,如果您有简短的方法和 Resharper 来为您突出显示内容,那么您完全不需要手动将内容声明为final/readonly/const(取决于语言)。 IDE 会为您跟踪它。这更有意义:正如 Java 程序员所发现的那样,大多数“变量”根本不是变量。但是,如果您必须通过向所有内容添加
final
来声明这一点,那么代码中就会产生大量额外的关键字噪音。将默认值设置为final
并使用关键字使事物可变
更有意义。这就是 F# 中的工作原理。There is a
readonly
keyword in C# but so far we can only apply it to fields, not locals or parameters.Often in Java the
final
keyword is used habitually as a matter of style, to make it easier to close over values in anonymous inner classes. But that problem doesn't exist in C# as anonymous methods/lambdas can close over modifiable variables.Also tools like Resharper can display mutated variables/parameters in a different colour, so you can see the "complications" in your method at a glance. I use it to make mutated names light up in bold green text or something like that!
This is not to say that C# itself wouldn't be improved by many more features like a widely applicable
readonly
to help with declared immutability. But if you have short methods and Resharper to highlight things for you, then you don't entirely need to manually declare things as final/readonly/const (depending on the language). The IDE tracks it for you.This makes more sense: as Java coders have discovered, most "variables" are not variables at all. But if you have to declare this by adding
final
to everything, that's a lot of extra keyword noise in your code. It would make more sense for the default to befinal
and to have a keyword to make thingsmutable
. This is how it works in F#.在安装了 Code Contracts 附加组件的 C# 4.0 / Visual Studio 2010 中,您可以在 ContractInvariantMethod 属性中使用 Contract.Invariant 来检查方法调用期间是否未修改值。
不完全相同,但我想,这已经是你能得到的最接近的了。
In C# 4.0 / Visual Studio 2010 with the Code Contracts add-on installed you can use Contract.Invariant in a ContractInvariantMethod attribute to check that a value is not modified during a method call.
Not quite the same thing but about as close as you'll get I think.
c#中没有这样的东西
there is no such thing in c#
C# 有一个 readonly 关键字,但它不能用于局部变量(仅限类成员)。
这意味着无法标记方法参数常量。
我也很怀念它,因为在我的 Java 代码中几乎每个参数和局部变量都是最终的(我相信这是一个很好的编码模式)。
C# has a readonly keyword but it cannot be uset to local variables (class members only).
This means that there is no way to mark a metheod parameter constant.
I miss it too cause in my Java code almost every parameter and local variable is final (which is a good coding pattern I believe).
正如其他人所说,C# 中没有这样的东西。
如果您的意图是禁止被调用的方法更改您的对象(或者确定它不会),那么您唯一的选择是传递对象的深层克隆。 - 不是很优雅,但这是唯一可以确定的方法。
As others stated, there's no such thing in C#.
If your intention is to forbid the called method to change your object (or know for sure it does not), your only option is to pass a deep clone of your object. - Not very elegant, but the only way possible to be sure.