C# generics example

Posted on 2009-02-17
Last Modified: 2013-12-14
What is the exact syntax to be used for a method using generics feature?

Response <Z> MyMethod( T t, U u) {
will the above syntax work?
Basically The Response type can be any derived type from a basic Response Type(class). The arguments T and U also can be any derived types from a basic type/class respectively.
Are there good online docs for genercis in C#
Thanks in advance
Question by:pudur2007
    1 Comment
    LVL 8

    Accepted Solution

    You are talking about constraints in generic type parameters - see - here are the basics:

    Ensure T is or is derived from class - where T : Person  
    Ensure T implements interface IInterface - where T : ISerializable
    Ensure T is newable (e.g. use new T() in function) - where T : new()
    Use multiple parameters - where T: new(), ICloneable, IComparable<U>
    You can declare your generic function as follows:

            public Z MyMethod<Z, T, U>(T t, U u)
                where Z : Response
                where T : MyClass
                where U : MyOtherClass

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now