简体   繁体   中英

Multiple aliases for same type in c#

I prefer Delegates as arguments wherever possible. So I usually come across situation where multiple arguments of a delegate are of same type.I am trying to make more readable. To achieve this, tried with c# keyword "using" directive.

using System;
using FirstNumber = System.Int32;
using SecondNumber = System.Int32;
using Result = System.Int32;

public class FunctionPointers
{
    Func<FirstNumber, SecondNumber, Result> add;

    public FunctionPointers(Func<FirstNumber, SecondNumber, Result> op)
    {
        add = op;
    }
}

智能感知快照 From the screen shot, it is clear that alias name is same for all arguments. Is there a way to correct this? or usage of aliases is incorrect?

I can't say I fully understand the rational behind your approach here, but it's important to consider that what you've essentially declared is that:

FirstNumber = SecondNumber = Result = System.Int32;

Being that all these identities now refer to the same type, System.Int32 . For that reason, visual studio is choosing to use the first author assigned allias to populate the intellisense.

When it comes to the declaration of delegates, consider that you simply defining the required signature, being the "IN and OUT" type(s). It is the method that is ultimately "wrapped"/"encapsulated" that will provide the identities of the parameters.

To achieve your readability, if indeed you want to go this way, consider defining your own delegate type with the "readable" identities you wanted:

public class FunctionPointers
{
    Operator Add;

     public FunctionPointers(Operator addOp)
     {
            Add = addOp;
     }       
}

public delegate Int32 Operator(Int32 FirstNumber, Int32 SecondNumber);

Otherwise, the "normal" way, with the identities on the method:

Func<Int32,Int32,Int32> add;

public FunctionPointers()
{
    add = AddMethod;
}

public Int32 AddMethod(Int32 FirstNumber, Int32 SecondNumber)
{
    return FirstNumber + SecondNumber;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM