Search code examples
c#compatibilityc#-6.0outc#-7.0

C# Out Discard, Make Compatible With C#6


I am modifying the source of a program in order to make it support the C# 6 compiler. The source is currently compatible with C# 7 and makes use of some of the new changes to syntax in C# 7. So obviously it won't compile with a C# 6 compiler.

I have for the most part finished everything.

for example, this line (not compatible with C# 6):

var type = transmissionType(freqDest, freqFactor, out var bytes);

had to be changed to this in order to be compatible with C# 6 compiler:

byte[] bytes;
var type = transmissionType(freqDest, freqFactor, out bytes);


But I have recently come across this line (not compatible with C# 6):

public byte[] Receive() => Receive(out _, out _, out _);

and I am not quite sure how I could rewrite/change this so it would be compatible with the C# 6 compiler. The out _ appears to be some type of discard implemented into C# since C# 7 was released.


Solution

  • A C# discard variable is basically a variable result from a function call that you know you will not use. You can just declare the variables and not use them.

    public byte[] Receive() 
    {
        get
        {
            var a, b, c;
            return Receive(out a, out b, out c);
        }
    }