luben111

Hello,

I have the following problem and I can not figure out how to solve it in the best C# style:

I need a class with 2 methods:


Re: Visual C# Language Constraints in generic clases

Figo Fei - MSFT

Hi, luben

I'm sorry for not getting exactly what you want.

But it seems that you should define the class like this:

        public class Calculate< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

        {

            public static T Average<T>(params T[] var) where T : Imine

            {

 

                ....

                return result;

            }

            public static T Median<T>(params T[] var) where T : Imine

            {

                ....

                return result;

 

            }

        }

 

        interface Imine

        {//define your interface

 

        }

And you must implement the interface in the class you transfer to "T". Otherwise, you dont need Imine.

If you have further problem, feel free to let me know.

Thank you






Re: Visual C# Language Constraints in generic clases

luben111

Hello,

Yes.... it is really too far away from what I want  (I'm completely unsatisfied from the answer).

Actually what you listed above you can find into every C# book  - it's just how to use the interfaces like constraints into generic methods. In  the example you gave us, there is realy no need to use interfaces (constraints), because the need of interfaces comes when you start operating with particular types of data.

My goal is different - I want to create multiple methods (like average(), median(), oversampling(), secondMax(), secondMin(), etc.) which take for example only types -  Byte, UInt16, UInt32 and ULong (but in fact they could be any combination of types or interfaces). To avoid writing every method many times with "override"  I would like to get advantage of generic types. It's easy to see that all these generic methods will require one and the same constraints.

So the question stays and I'll be thankfull if you can ask some of the Microsoft "guru" in C# around you.

Thank you

Best regards






Re: Visual C# Language Constraints in generic clases

Figo Fei - MSFT

Hi,

If I misunderstand you anyway, pls tell me.

You want create methods with Byte, UInt16, UInt32 and ULong as their param, right If so, you can define the methods as I previously described, additionally add a custom attribute to each method. At least, the definition in your above code does not make sense.

Thank you for discussion.






Re: Visual C# Language Constraints in generic clases

luben111

Hello,

I really want to create generic methods which takes Byte, UInt16, UInt32 and ULong.

OK, to describe it in words -  I need a FAMILY of methods (many methods which have something common between them) which take let's say Byte, UInt16, UInt32 and ULong types of parameters ( but don't accept string, float, double, etc. and generate errors in compilation time in case you accidentally tried to use such types).

I can not figure out how exactly to build the interface (used for the constraints) and how exactly then to use it inside the method - how exactly to pass the value, typecast it to the desired type, how to use it later inside the body of the method.

As you said - "....as I previously described, additionally add a custom attribute... "  - the question is  HOW I can not build the whole solution - looks like all mentioned ideas should work, but when you try to put them together you'll see that something does not fit.

So, as they say ... "the devil is in the details"...

Thank you

Best regards






Re: Visual C# Language Constraints in generic clases

Figo Fei - MSFT

Hi, luben

By using attributes, it will create alert info in compiling time. I think it can do what you want according to your description.

For example:

public class Class
{
    [Obsolete("Don't use this old method, use New method", true)]
    static void Old( ) { }
  
    static void New( ) { }
  
    public static void Main( )
    {
        Old( );
    }
}


When we try to compile above program, we will get an error: AnyClass.Old()' is obsolete: 'Don't use Old method,  use New method'

So you can use it to avoid whatever invalid methods which you dont want to use.

Forgive me not showing you all the code. You can reference to C# attributes and custom attributes.

Thank you






Re: Visual C# Language Constraints in generic clases

luben111

Hello,

It's a kind of protection against using wrong type, but is not the solution I was looking for (attributes are as they said: "An attribute is a piece of additional declarative information that is specified for a declaration."

What I need is not additional information (which will kinda patch the main problem) but streight and C# style approach to ensure that the parameters I passed to the generic method are correct and what is more important - to be able to pass, typecast and use the values of the parameters inside the method.

I was not aware of the atributes and seems that they can improve further the C# structure of the program, but they have different meaning and philosophy. For sure they are better approach then checking the type inside the generic method and rising error in run time, because attributes rise the error in the compilation time. What they can not do is to offer me some C# approach to pass and use the parameters. If you try implement the method Average<T>(var1....) from A to Z you'll understand what I'm talking about.

So, I'm continue searching the right solution.

Best regards






Re: Visual C# Language Constraints in generic clases

Figo Fei - MSFT

OK, So I feel you'd overload a method for each type.

Thanks.






Re: Visual C# Language Constraints in generic clases

sharpMoon

:) i think the question comes to the beginning point.

to avoid the overload or not






Re: Visual C# Language Constraints in generic clases

James Curran

Figo Fei - MSFT wrote:

interface Imine

{//define your interface

}

And you must implement the interface in the class you transfer to "T". Otherwise, you dont need Imine.

The problem is that a) we would need IMine defined like this:
interface Imine
{
static Imine operator+(Imine a, Imine b);
static Imine operator-(Imine a, Imine b);
static Imine operator*(Imine a, Imine b);
static Imine operator/(Imine a, Imine b);
}

which is impossible. and B) even it such an interface were possible and did exist, we'd need to have int, short, float etc, defined as implementing that interface. The fact that no such interface exists is a MAJOR failing of the CLR/generics in .Net






Re: Visual C# Language Constraints in generic clases

Figo Fei - MSFT

Hi,

What generics can do is to let more types be compatible and valid, and generics is just one of the specifications in C#.






Re: Visual C# Language Constraints in generic clases

luben111

Hello,

Yes, generics let more types to be compatible and valid.... the question was - how I can restrict all these possible types to the types I really need. For example in my particular case there was no sense to calculate Average value on strings or booleans, I need only to do this on Byte, UInt16, UInt32 and ULong (even I don't want signed types).

OK, let's put it in other words - if you have personally to write a generic method to calculate the average value of several variables (only types Byte, UInt16, UInt32 and ULong) how you'll write it

Keep in mind that I'm familiar with the approach with "override", but I don't want to use it because I have to create a "family" of methods and all they will use the same restrictions of the types (override approach needs to rewrite many times every method, which sometimes could be extremely complex - up to 2000 lines code!). It's obvious that the most elegant solution is generic, the question is HOW.

Best regards






Re: Visual C# Language Constraints in generic clases

Figo Fei - MSFT

Hi,

I'm afraid there's no easy and direct solution to the exact request, but there can be some tradeoffs, like using attributes, using where T: struct (since the types are all value types) or using multiple overloads.

Thanks






Re: Visual C# Language Constraints in generic clases

Matthew Watson

See here for more info:

http://www.codeproject.com/csharp/genericnumerics.asp




Re: Visual C# Language Constraints in generic clases

luben111

Thank you so much Matthew!

It's exactly the answer of my question, despite that it's too complex (I guess Microsoft will improve this soon). I knew that I'm not the first one who is fighting with this problem. Maybe I'll use in my project now the "override" approach - seems that the added complexity of generics and the decreased performance are worse then to rewrite the routines many tuimes with override. The good thing is - I know now that such approach with generics exists and in some cases maybe this will be the most elegant and optimal solution.

Thank you Matthew again for the help!

Best regards

Luben