C# Shorten syntax when repeatedly using type in generics?
Asked Answered
N

6

9

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.

Nicholenicholl answered 11/7, 2012 at 13:1 Comment(2)
How about changing the name of the CompanyLookupData Type to something shorter. anything else, would make your code less readableCrissy
I could, but I don't want to rename my classes every time I need to use them in a delegate, haha. :)Nicholenicholl
K
7

Yes, there are a few ways to achieve this:

If the variable is a local variable you can use the var keyword:

var DELCompareNonKeyFieldsCompany = new Func<CompanyLookupData, CompanyLookupData, bool>   (CompanyLookupData.HasIdenticalNonKeyFieldsTo);

However, if DELCompareNonKeyFieldsCompany is 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 a Func:

Func<CompanyLookupData, CompanyLookupData, bool> DELCompareNonKeyFieldsCompany = CompanyLookupData.HasIdenticalNonKeyFieldsTo;

If this type is to be used often, you may wish to create your own delegate type:

public delegate bool CompareCompanyNonKeyFields(CompanyLookupData, CompanyLookupData);

And use it like so:

CompareCompanyNonKeyFields DELCompareNonKeyFieldsCompany = CompanyLookupData.HasIdenticalNonKeyFieldsTo;

Alternatively if the type is only to be used within the one class, you could also create an alias to the type with the using keyword (although, personally, I find that this hinders the readability of the code):

using CompareCompanyNonKeyFields = System.Func<CompanyLookupData, CompanyLookupData, bool>;
...
 CompareCompanyNonKeyFields DELCompareNonKeyFieldsCompany = CompanyLookupData.HasIdenticalNonKeyFieldsTo;
Killy answered 11/7, 2012 at 13:3 Comment(1)
Instead of "field" your probably meant "property"?Ynez
B
4

Take the using statement to declare an alias:

using MyType = System.Func<CompanyLookupData, CompanyLookupData, bool>;

MyType DELCompareNonKeyFieldsCompany = new MyType(CompanyLookupData.HasIdenticalNonKeyFieldsTo);
Boyhood answered 11/7, 2012 at 13:6 Comment(0)
T
3

You can hide it in an helper factory method:

void Main() {
    var f1 = Helper.Create(CompanyLookupData.HasIdenticalNonKeyFieldsTo);
    var f2 = Helper.Create(CompanyLookupData.HasIdenticalNonKeyFieldsTo);
}

static class Helper {
    public static Func<T, T, bool> Create<T>(Func<T, T, bool> @delegate) {
        return @delegate;
    }
}
Teledu answered 11/7, 2012 at 13:7 Comment(0)
D
2

The only way I can think of is a using declaration.

using cld = MyNamespace.CompanyLookupData;

Then you can use cld in place of the full name in the rest of the file.

Derron answered 11/7, 2012 at 13:3 Comment(0)
S
2

You can use var, to shorten a little :)

var DELCompareNonKeyFieldsCompany = new Func<CompanyLookupData, CompanyLookupData, bool>(CompanyLookupData.HasIdenticalNonKeyFieldsTo); 
Sophister answered 11/7, 2012 at 13:3 Comment(0)
D
0

No, you cannot do something fundamentally better than this. For better or for worse, C# does not have the moral equivalent of typedef.

What you might try is a using alias to shorten the type name, although personally I believe this is not a good idea:

using CLD = CompanyLookupData;

Func<CLD, CLD, bool> DELCompareNonKeyFieldsCompany =
    new Func<CLD, CLD, bool> (CLD.HasIdenticalNonKeyFieldsTo);

Another possibility (far less likely to be applicable in practice) is to use an implicitly typed variable (var) if we are talking about a local.

Dwain answered 11/7, 2012 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.