向公共类添加公共方法是一项重大更改吗? [英] Is adding a public method to a public class a breaking change?

查看:24
本文介绍了向公共类添加公共方法是一项重大更改吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如一个 Person 只有一个 Age 方法:

For example a Person that has only one Age method:

public class Person
{
    public int Age()
    {
        return 6;
    }
}

添加了Height()方法.

public class Person
{
    public int Age()
    {
        return 6;
    }
    public int Height()
    {
        return 6;
    }
}

这是一个突破性的变化吗?注意Person不是密封的.

Is that a breaking change? Pay attention that Person is not sealed.

推荐答案

tl;dr;

埃里克·利珀特是对的,再次......

Eric Lippert is right, again..

感谢 Tim Schmelter 的评论以及指向 Eric Lippert 的 博文,我已经改变了我原来的答案.

Thanks to Tim Schmelter's comment with the link to Eric Lippert's blog post, I've changed my original answer.

向现有类添加公共方法或属性可能是一项重大更改.可能不是,但可能是!

Adding a public method or property to an existing class is potentially a breaking change. Probably not, but it might be!

更长的版本

重大更改意味着您正在更改您的类型的公共 API,以致于无法再编译使用当前 API 的现有代码.

A breaking change means you are changing the public API of your type in such a way that existing code working with the current API will no longer be able to compile.

明显的破坏性更改是删除公共成员,或以这样的方式更改它们,以至于无法再编译使用您的类的代码.此类更改可能是(部分列表!):

Obvious breaking changes are removing public members, or altering them in such a way that the code using your class can't be compiled anymore. Such changes might be (a partial list!):

  • 向现有方法添加非可选参数.*
  • 将现有方法的参数的数据类型更改为不是原始参数的基类型的数据类型
  • 将属性的数据类型更改为不是从原始数据类型派生的数据类型
  • 将方法的返回类型更改为不是从原始类型派生的类型

正如 Eric 所证明的那样,即使添加一个新的公共方法也潜在是一个破坏性的更改,就像他在博客中提到的破坏性重载一样,将属性或参数的类型更改为更具体的类型也是如此(源自原始类型的意思)也可能是一个突破性的变化,原因完全相同.

As Eric demonstrates, even adding a new public method is potentially a breaking change, and just like in the breaking overload he blogged about, so does changing types of properties or arguments to a more specific types (meaning derived from original types) might also be a breaking change, for the exact same reasons.

但是,Eric 还指出,此类场景非常具体且不太可能,因此您可以假设您不会遇到它们.

However, Eric also points out that such scenarios are so specific and unlikely, that it's probably safe to assume you will not encounter them.

这种破坏性变化的特殊味道"很奇怪,因为它几乎使一种类型的表面积的所有可能变化都变成了潜在的破坏性变化,同时又是一种明显人为且不太可能的情况,以至于没有现实世界"的开发人员可能会遇到它.

This particular "flavour" of breaking change is an odd one in that it makes almost every possible change to the surface area of a type into a potential breaking change, while at the same time being such an obviously contrived and unlikely scenario that no "real world" developers are likely to run into it.

<小时>

*虽然我们讨论的是重大更改,但更改可选参数默认值也是一个潜在的重大更改 - 因为可选参数的工作原理是将默认值注入调用方法作为传递给具有可选参数.更改默认值意味着您必须重新编译使用该方法的所有内容.


*While we are on the subject of breaking changes, changing an optional argument default value is also a potential breaking change - since optional arguments works by injecting the default value to the calling method as an argument passed to the method that have an optional argument. Changing the default value means you have to recompile everything that uses the method.

这篇关于向公共类添加公共方法是一项重大更改吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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