为什么System.Windows.Point和放大器; System.Windows.Vector可变的? [英] Why are System.Windows.Point & System.Windows.Vector mutable?

查看:219
本文介绍了为什么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?

我想明白这一点,所以我可以决定是否将是有意义的使自己的类似结构可变的(如果有的话)。这是可能的决定,使向量可变只是在判断上的错误,但是如果有一个很好的理由(例如,提高性能),我想知道这是什么。

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()方法了几次,因为它,惊喜(!),不返回新鲜的实施向量。它只是改变了电流矢量。

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 EX pressed。不幸的是有没有办法来调用使用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.

这是一件坏事?对于这些类型,我会说没有。他们只是用于保存数据,仅此而已。如果你想获得的大小窗口想成为,你访问 DesiredSize 获得尺寸对象重新presenting它想要的大小。你并不意味着通过改变宽度高度改变所需的大小的<$ C性能$ C>你得到大小对象,通过提供一个新的尺寸对象改变大小。看着这种方式是很多更自然的,我相信。

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天全站免登陆