Tech Off Thread

9 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Can't do Func ?

Back to Forum: Tech Off
  • User profile image
    brian.​shapiro

    You can't do Func<out char, int> ?

    Is there a correct syntax for representing a function that takes a reference as a parameter, or is this not allowed?


  • User profile image
    evildictait​or

    The first parameter of Func<TResult, TArg1> is the return type of the function, so no, you can't do that.


    Func<char, int> corresponds to a function with signature

    char foo(int parameter) {}

  • User profile image
    brian.​shapiro

    evildictaitor said:
    The first parameter of Func<TResult, TArg1> is the return type of the function, so no, you can't do that.

    Func<char, int> corresponds to a function with signature

    char foo(int parameter) {}
    evildictator,

    The syntax shown in Visual Studio is Func<T, TResult> where T is a parameter.

    Either way, it won't let me pass in a type parameter by reference.

  • User profile image
    dpratt71

    brian.shapiro said:
    evildictaitor said:
    *snip*
    evildictator,

    The syntax shown in Visual Studio is Func<T, TResult> where T is a parameter.

    Either way, it won't let me pass in a type parameter by reference.

    You have the parameter order correct, but you still can't do what you want. The only thing a generic parameter can express is a type. The The parameter direction is defined by the Func<> delegate itself, and you can't change it. Depending on what you're trying to do, you can define your own delegate apart from Func<>. Something like this, perhaps:

    public delegate int MyDelegate(out char value);

    Actually, I'm not 100% sure the above is valid. An "out" parameter on a delegate may not be valid.

  • User profile image
    joechung

    dpratt71 said:
    brian.shapiro said:
    *snip*
    You have the parameter order correct, but you still can't do what you want. The only thing a generic parameter can express is a type. The The parameter direction is defined by the Func<> delegate itself, and you can't change it. Depending on what you're trying to do, you can define your own delegate apart from Func<>. Something like this, perhaps:

    public delegate int MyDelegate(out char value);

    Actually, I'm not 100% sure the above is valid. An "out" parameter on a delegate may not be valid.
    That works.

  • User profile image
    Yggdrasil

    I think what you're looking for, to elaborate on what dpratt has written, is a new delegate along these lines:

    public delegate void OutFunc<TIn, TOut> (TIn inParam, out TOut outParam); 
    

     

  • User profile image
    TommyCarlier

    Yggdrasil said:

    I think what you're looking for, to elaborate on what dpratt has written, is a new delegate along these lines:

    public delegate void OutFunc<TIn, TOut> (TIn inParam, out TOut outParam); 
    

     

    That's a bad idea. Your delegate has 1 output value. It should be a regular function that RETURNS the output value. The .NET design guidelines state that ref- and out-parameters should only be used if there's no other option.

  • User profile image
    littleguru

    brian.shapiro said:
    evildictaitor said:
    *snip*
    evildictator,

    The syntax shown in Visual Studio is Func<T, TResult> where T is a parameter.

    Either way, it won't let me pass in a type parameter by reference.

    What does the type have to do with the way how it is passed?

  • User profile image
    Yggdrasil

    TommyCarlier said:
    Yggdrasil said:
    *snip*
    That's a bad idea. Your delegate has 1 output value. It should be a regular function that RETURNS the output value. The .NET design guidelines state that ref- and out-parameters should only be used if there's no other option.

    Jeez, can't anybody focus on the issue at hand? Of course it's a pointless method signature, but it serves to illustrate the point of how to represent a generic delegate with an out param, which is what the original poster wanted. A technical question gets a technical answer.
    Would you also like to correct my coding standards, because it's considered unnecessary to add the T prefix to the type definitions?

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.