Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit parameter modifiers with delegates? (C#)

Tags:

c#

I'm working on a library that allows users to instantiate some delegates.

I have a defined a delegate type that deals with a struct as one of its parameters, that we only need to read and not modify, so the in keyword seemed useful.

public struct SomeStruct 
{ 
    public string x; 
    // and possibly more...
}

public delegate void MyDelegate(object foo, in SomeStruct bar);

However, when I go to create it, it tells me I need to put in there.

// Parameter 2 must be declared with the 'in' keyword
MyDelegate x = (foo, bar) => System.Console.WriteLine(bar.x);

If I use in, I now have to explicitly type the parameter...

// doesn't work
MyDelegate x = (foo, in bar) => System.Console.WriteLine(bar.x);

But now that I explicitly type the second parameter, the first parameter needs to be explicit too.

// Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit
MyDelegate x = (foo, in SomeStruct bar) => System.Console.WriteLine(bar.x);
// ok
MyDelegate x = (object foo, in SomeStruct bar) => System.Console.WriteLine(bar.x);

Now in my use case, the types for parameters foo and bar could have long names, generic parameters, etc.

The type information is already there where this delegate type is defined. Is there a way to avoid making my users explicitly type the parameters for this delegate?

I expected (foo, bar) => or (foo, in bar) => to work but it does not.

edit: I was playing around more and this seems to be the case for ref as well, I'm guessing for all parameter modifiers. So question name was changed from just asking about in to modifiers in general

like image 600
James Avatar asked Jul 09 '21 05:07

James


People also ask

How do you pass parameters to delegates?

You can pass methods as parameters to a delegate to allow the delegate to point to the method. Delegates are used to define callback methods and implement event handling, and they are declared using the “delegate” keyword. You can declare a delegate that can appear on its own or even nested inside a class.

What is delegate C#?

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.

Why delegates why not call methods directly?

If you think of delegates as being similar to interface definitions for a specific type of method, you can start to see why delegates exist. They allow clients of our delegates to ignore all the details of their implementations - even their names!


Video Answer


1 Answers

No, you can't provide modifiers with implicit anonymous function parameters.

If you look at the grammar in the ECMA standard - which is currently for C# 6, hence the lack of the in modifier - you'll see the difference between explicit_anonymous_function_parameter which includes both an optional modifier and a type, and an implicit_anonymous_function_parameter which is just an identifier:

explicit_anonymous_function_parameter
    : anonymous_function_parameter_modifier? type Identifier
    ;

anonymous_function_parameter_modifier
    : 'ref'
    | 'out'
    ;

implicit_anonymous_function_parameter
    : Identifier
    ;

I agree this can be slightly frustrating - but I wouldn't expect to see it change any time soon.

like image 81
Jon Skeet Avatar answered Oct 26 '22 23:10

Jon Skeet