本文介绍了在C#中具有丰富的类型const正确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自一个C ++的背景和努力学习C#中,我已经遇到的最令人沮丧的语言遗漏一个是相当于常量关键字。

Coming from a C++ background and trying to learn C#, one of the most frustrating language omissions that I've come across is an equivalent to the const keyword.

所以,我一直在试图解决的,我可以用它来实现const正确性在C#中的模式。

So, I have been attempting to settle on a pattern that I can use to achieve const correctness in C#.

这个答案有一个有趣的建议:创建一个只读接口为所有类型的。但马特·克鲁克香克指出,在评论中,这成为问题,如果你的类有收藏或其他丰富的类型。特别是如果你没有过度的类型控制,并且不能使其实现只读接口

This answer has an interesting suggestion: create a read only interface for all of your types. But as Matt Cruikshank pointed out in the comments, this becomes problematic if your class has collections or other rich types. Particularly if you do not have control over the type, and can't make it implement a read-only interface.

是否有任何图案或解决方案存在可处理丰富的类型和集合,还是我们被迫在C#中简单地进行复制?难道不如干脆放弃对const正确性在C#中共有?

Do any patterns or solutions exist that can handle rich types and collections, or are we forced in C# to simply make copies? Is it better to just give up on const correctness in C# altogether?

推荐答案

您可以得到不变性在C#中?当然,如果你设计的它。你可以做创造性的东西与接口等,仅露出的 GET 属性和没有一个可变的方法。

Can you get immutability in C#? Sure, if you design for it. You can do creative things with interfaces, and so on, to only expose the get of properties and none of the mutable methods.

不过,要记住没有什么是$ P $从铸造回的实际类型pvents一个狡猾的用户(当然,同样可以说,C ++的,你可以抛弃常量性)。

That said, keep in mind there is nothing that prevents a crafty user from casting it back to the actual type (of course, same could be said of C++, you can cast away const-ness).

ISomeReadOnlyInterface readOnly = new SomeFullObject();

// hah, take that read-only interface!
((SomeFullObject)readOnly).SomeMutatingMethod();

同样的,集合。即使你返回ReadOnlyCollection还(其中prevents变异集合本身的行为)集合中的数据仍然是可变的(只要该类型允许,当然它)。

Same with collections. Even if you return a ReadOnlyCollection (which prevents mutating behaviors on the collection itself) the data in the collection is still mutable (as long as the type allows it of course).

所以恐怕真的在这里没有简单的答案。有没有倒装一个开关常量,给你什么C ++一样。

So I'm afraid there's really no simple answer here. There's no "flip-a-switch" const that gives you what C++ does.

这真的取决于你,你可以:

It's really up to you, you can:

  • 在设计你的类型是不可改变的,返回,而不是可变集合迭代器(或其他只读序列)。
  • 在每次返回新副本,这样,如果他们改变他们来说,根本不算什么。
  • 刚返回的实际数据,并留下篡改行为是不确定的。
  • 等等...

后者是集合像词典< TKEY的,TValue> 做的。没有什么,说你不能使按键类型可变类型(但如果你这样做荣辱与共),和MSDN是pretty的清楚,如果你改变以这样的方式的关键,它的散列code变化,这是你自己的脖子上......

The latter is what collections like Dictionary<TKey, TValue> do. There's nothing that says you can't make the key type a mutable type (but woe if you do), and the MSDN is pretty clear that if you alter the key in such a way that it's hash code changes, it's on your own neck...

有关我自己的工作,我倾向于保持简单,除非有实际上是一个大问题我的课可能在某种程度上会导致副作用的改变。举例来说,如果我是存储Web服务结果高速缓存中,我将返回缓存项的副本,而不是这样,如果一个用户修改的结果,他们不会在不经意间改变缓存的值。

For my own work, I tend to keep it simple unless there is actually a big concern my class may be altered in a way that would cause side-effects. For example, if I'm storing web service results in a cache, I'll return a copy of the cached item instead so that if a user modifies the result they won't inadvertently modify the cached value.

因此​​,长期和短期的是,我不会担心的常量,正确性的每次的键入返回,这只是太多。我只担心你回来,如果改变,可以创建一个副作用给其他用户的事情。

So, long and the short of it is that I wouldn't worry about const-correctness of every type you return, that's just way too much. I'd only worry about things that you return that, if altered, could create a side-effect to other users.

这篇关于在C#中具有丰富的类型const正确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:15