Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegate returning type object does not accept function returning Guid

Tags:

c#

When returning the following function

public Guid GetUserFilterValue( string filterID )

As

Func<string, object> 

I am getting the error that it has the wrong return type. If I change the return type to anything other than Guid it compiles. Any ideas?

like image 568
Stewart Alan Avatar asked Jun 12 '26 13:06

Stewart Alan


1 Answers

This would usually work if the return type was a reference type, because Func<T, TResult> is covariant for T; but Guid is a value type, this is why your method is not a valid candidate for Func<string, object>.

From MSDN:

Variance applies only to reference types; if you specify a value type for a variant type parameter, that type parameter is invariant for the resulting constructed type.

As a workaround, you could use a lambda expression:

Func<string, object> f = filterId => GetUserFilterValue(filterId);
like image 97
Thomas Levesque Avatar answered Jun 14 '26 04:06

Thomas Levesque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!