Overriding methods with tuples and field name rules in C # 7.0

I read this page , which is officially mentioned in the release notes in Visual Studio 17 RC, it states the following:

For the purpose of overloading Overriding Hiding tuples of the same type and length, as well as their basic types, ValueTuple is considered equivalent. All other differences are not significant.

When redefining a member, it is allowed to use tuple types with the same or different field names than in the base element.

The situation when the same field names are used for mismatched fields between the base and derived signatures of participants, a warning is reported to the compiler

When creating this snapshot:

public abstract class Foo { public abstract void AbstractTuple((int a, string b) tuple); public virtual void OverrideTuple((int a, string b) tuple) { Console.WriteLine($"First= {tuple.a} Second = {tuple.b}"); } } public class Bar : Foo { public override void AbstractTuple((int c, string d) tuple) { Console.WriteLine($"First= {tuple.c} Second= {tuple.d}"); } public override void OverrideTuple((int c, string d) tuple) { base.OverrideTuple(tuple); } } 

I get the following errors:

Error CS8139 'Bar.AbstractTuple ((int c, line d))': tuple element names cannot change when overriding the inherited member 'Foo.AbstractTuple ((int a, line b))'

and

Error CS8139 'Bar.OverrideTuple ((int c, line d))': tuple element names cannot be changed when overriding the inherited member 'Foo.OverrideTuple ((int a, line b))'

Questions:

  • Is the official design error incorrect? Or is this behavior that has yet to be implemented in the official version of C # 7.0?

  • If this is the correct behavior, does it make sense that the root field names should be the same in the overridden method? Bearing in mind that two methods with the same Tuples (int a, int b) and (int c, int d) not considered overloaded candidates and generate an error!

  • Do we have official documentation on C # 7.0 features?

+6
source share
1 answer

Are official design errors wrong? Or is this behavior that has yet to be implemented in the official version of C # 7.0?

As far as I can tell, the documentation is out of date. This document has not changed significantly since April 2016 , and the error you received ( ERR_CantChangeTupleNamesOnOverride in the Roslyn source) was introduced in August 2016 (do not confuse it with another error code that was changed later).

If this is the correct behavior, does it make sense that the root field names should be the same in the overridden method? Holding the possibility that two methods with the same Tuples (int a, int b) and (int c, int d) not considered candidates for overloading and generate an error!

Yes, this looks like a new intentional behavior.

Do we have official documentation on C # 7.0 features?

Given that C # 7.0 is still under development, the documentation is a problem. An article on tuples in the official .Net documentation is currently being reviewed, although it does not mention these issues at all.

So, the outdated document you found is probably the best source at the moment. I created a problem asking me to update the documentation .

+3
source

Source: https://habr.com/ru/post/1012688/


All Articles