If I’m using generics, like in this sample case, is there a way to shorten the syntax so I don’t have to repeatedly type “CompanyLookupData”?
Func<CompanyLookupData, CompanyLookupData, bool> DELCompareNonKeyFieldsCompany =
new Func<CompanyLookupData, CompanyLookupData, bool>
(CompanyLookupData.HasIdenticalNonKeyFieldsTo);
I had tried to do Type t = typeof(CopmanyLookupData), and use t in all of the locations, but that doesn’t appear to work.
PS: while I’m open to a cleaner way of doing exactly what’s shown, I’m more interested in a way to make generics syntax more concise in general.
Yes, there are a few ways to achieve this:
If the variable is a local variable you can use the
varkeyword:However, if
DELCompareNonKeyFieldsCompanyis a class variable (a field or a property) you can let the compiler infer some of it for you by converting from a method group to aFunc:If this type is to be used often, you may wish to create your own delegate type:
And use it like so:
Alternatively if the type is only to be used within the one class, you could also create an alias to the type with the
usingkeyword (although, personally, I find that this hinders the readability of the code):