C#中的参数名称更改是运行时重大更改吗? [英] Is a parameter name change in C# a runtime breaking change?

查看:48
本文介绍了C#中的参数名称更改是运行时重大更改吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了几篇文章说,随着C#3.0中引入命名实参,参数名现在已成为公共合同的一部分.是真的,这是什么意思?我运行了一个简单的测试,更改MyLib.dll中的参数名称并没有破坏MyApp.exe,后者使用具有原始名称的命名参数调用了该方法,我认为是因为C#编译器在编译时进行了重载解析,并且生成的IL对参数名称一无所知.这是反汇编后的代码在Reflector上的样子:

I read a couple of articles that say that with the introduction of named arguments in C# 3.0, the name of parameters are now part of the public contract. Is this true, and what does it mean exactly? I ran a simple test and changing a parameter name in MyLib.dll did not break MyApp.exe which called the method using a named argument with the original name, I think because the C# compiler did overload resolution at compile time, and the generated IL knows nothing about the parameter name. This is what the disassembled code looks on Reflector:

private static void Main()
{
    bool CS$0$0000 = true;
    Class1.DoSomething(CS$0$0000);
    Console.ReadKey();
}

...这是原始源代码:

...and this is the original source code:

static void Main() {

    MyLib.Class1.DoSomething(a: true);

    Console.ReadKey();
}

推荐答案

我读了几篇文章说,随着C#3.0中引入命名实参,参数名现在已成为公共合同的一部分.这是真的,这到底是什么意思?

I read a couple of articles that say that with the introduction of named arguments in C# 3.0, the name of parameters are now part of the public contract. Is this true, and what does it mean exactly?

确实,参数名称是公共合同的一部分,但该语句包含两个错误.

It is true that argument names are part of the public contract, but the statement contains two errors.

最明显的错误是命名参数是在C#4.0中引入的,而不是在C#3.0中引入的.

The most obvious error is that named arguments were introduced in C# 4.0, not C# 3.0.

但是,更细微的错误更为重要.在C#4.0中引入命名参数并没有现在使参数名称成为方法公共合同的一部分.参数名称始终是公共合同的一部分,因为C#以外的语言都具有命名实参功能.尤其是,VB一直支持命名实参.

But the more subtle error is far more important. The introduction of named arguments in C# 4.0 did not now make names of parameters part of the public contract of a method. Names of parameters were always part of the public contract because languages other than C# had the named arguments feature. In particular, VB has always supported named arguments.

因此,在库中更改参数名称总是 ,这是VB重新编译的重大更改.现在,这也是C#重新编译的重大突破,但这并不意味着它以前很安全,现在很危险.总是很危险.

Therefore changing the name of a parameter in a library was always a breaking change for VB recompilation. Now it is also a breaking change for C# recompilation, but that doesn't mean that it was safe before and now it is dangerous. It was always dangerous.

C#并不是唯一的.NET语言,目前为止还不是.当您更改某种类型的公开可见表面的一部分时,您可能会冒用每种语言破坏程序的风险.具有或不具有功能的C#不会改变这一事实.

C# is not the only .NET language, not by far. When you change part of the publically visible surface of a type, you risk breaking programs in every language. C# having or not having a feature doesn't change that fact.

这篇关于C#中的参数名称更改是运行时重大更改吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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