为什么 System.Windows.Point &System.Windows.Vector 可变吗? [英] Why are System.Windows.Point & System.Windows.Vector mutable?

查看:15
本文介绍了为什么 System.Windows.Point &System.Windows.Vector 可变吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于可变结构通常被认为是邪恶的(例如,为什么可变结构是邪恶的"?),是否有潜在的好处可能促使 .NET 框架的设计者制作 System.Windows.Point &System.Windows.Vector 可变吗?

Given that mutable structs are generally regarded as evil (e.g., Why are mutable structs "evil"?), are there potential benefits that might have prompted the designers of the .NET framework to make System.Windows.Point & System.Windows.Vector mutable?

我想了解这一点,以便我可以决定使我自己的类似结构可变(如果有的话)是否有意义.使 PointVector 可变的决定可能只是判断错误,但如果有充分的理由(例如,性能优势),我希望了解它是什么.

I'd like to understand this so I can decide whether it would make sense to make my own similar structs mutable (if ever). It's possible the decision to make Point and Vector mutable was just an error in judgment, but if there was a good reason (e.g., a performance benefit), I'd like to understand what it was.

我知道我在 Vector.Normalize() 方法的实现上偶然发现了几次,因为它,令人惊讶的是(!),并没有返回一个新的 Vector.它只是改变了当前的向量.

I know that I've stumbled over the implementation of the Vector.Normalize() method a few times because it, surprise (!), does not return a fresh Vector. It just alters the current vector.

我一直认为它应该是这样工作的:

I always think it should work like this:

var vector = new Vector(7, 11);
var normalizedVector = vector.Normalize(); // Bzzz! Won't compile

但它实际上是这样工作的:

But it actually works like this:

var vector = new Vector(7, 11);
vector.Normalize(); // This compiles, but now I've overwritten my original vector

...所以,似乎不变性只是为了避免混淆是一个好主意,但同样,在某些情况下,潜在的混淆可能值得.

...so, it seems like immutability is a good idea simply for avoiding confusion, but again, perhaps it's worth that potential confusion in some cases.

推荐答案

这些类型位于 System.Windows 命名空间中,通常用于 WPF 应用程序.应用程序的 XAML 标记是框架的重要组成部分,因此对于很多事情,它们需要一种使用 XAML 表示的方法.不幸的是,无法使用 WPF XAML 调用非无参数构造函数(但在松散 XAML 中是可能的),因此尝试使用适当的参数调用构造函数来初始化它是不可能的.你只能很自然地设置对象属性的值,这些属性需要是可变的.

These types are in the System.Windows namespace and are generally used in WPF applications. The XAML markup of an application is a big part of the framework so for a lot of things, they need a way to be expressed using XAML. Unfortunately there's no way to invoke non-parameterless constructors using WPF XAML (but it is possible in loose XAML) so trying to call a constructor with the appropriate arguments to initialize it wouldn't be possible. You can only set the values of the object's properties so naturally, these properties needed to be mutable.

这是件坏事吗?对于这些类型,我会说不.它们仅用于保存数据,仅此而已.如果您想获得 Window 想要的大小,您可以访问 DesiredSize 以获取表示它想要的大小的 Size 对象.您并不是要通过更改您获得的 Size 对象的 WidthHeight 属性来更改所需的大小",而是更改通过提供一个新的 Size 对象来调整大小.我相信这样看会自然得多.

Is this a bad thing? For these types, I'd say no. They are just for holding data, nothing more. If you wanted to get the size a Window wanted to be, you'd access the DesiredSize to get the Size object representing the size it wanted. You're not meant to "change the desired size" by altering the Width or Height properties of the Size object you get, you change the size by providing a new Size object. Looking at it this way is a lot more natural I believe.

如果这些对象更复杂并且执行更复杂的操作或具有状态,那么是的,您不会希望使这些类型既不可变也不结构.然而,由于它们只是尽可能简单和基本(本质上是一个 POD),因此结构在这里是合适的.

If these objects were more complex and did more complicated operations or had state, then yes, you wouldn't want to make these types neither mutable nor structs. However since they're just about as simple and basic as it can get (essentially a POD), structs would be appropriate here.

这篇关于为什么 System.Windows.Point &System.Windows.Vector 可变吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆