Results 1 to 8 of 8

Thread: C# generic constraint

  1. #1

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    C# generic constraint

    How to create a generic constraint, that only allows some predefined basic data types. Something like this:

    Code:
    T SomeFunction<T>() where T : int, double, string, Guid
    {
        // ...
    
        return (T)someValue; // someValue can be of type: int, double, string or Guid
    }
    So, is this even possible?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: C# generic constraint

    That's not valid C# code. You can't specify actual value types as generic constraints. Here is the relevant documentation for C#:

    http://msdn.microsoft.com/en-us/library/bb384067.aspx
    http://msdn.microsoft.com/en-us/library/d5x73970.aspx

    The VB equivalent of:
    csharp Code:
    1. T SomeFunction<T>() where T : Type1, Type2
    is:
    vb.net Code:
    1. Function SomeFunction(Of T As {Type1, Type2})() As T
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: C# generic constraint

    Actually, I think I may have missed the main point of your question. I think you were asking if it is possible to specify that the generic type parameter satisfies ANY of the specified constraints. The answer is no. If there are multiple constraints then the generic type parameter must satisfy ALL of them. To handle multiple types like that you would have to overload the method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: C# generic constraint

    Quote Originally Posted by jmcilhinney View Post
    To handle multiple types like that you would have to overload the method.
    Ok. And how would I go about that?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: C# generic constraint

    Quote Originally Posted by gavio View Post
    Ok. And how would I go about that?
    You would open a search engine, type in overload method c# and then search. If you can't find what you need or can't understand what you find, then you'd post back here. There's gigabytes of information out there already. Don't ignore it. Look first, ask questions later.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: C# generic constraint

    Quote Originally Posted by jmcilhinney View Post
    You would open a search engine, type in overload method c# and then search. If you can't find what you need or can't understand what you find, then you'd post back here. There's gigabytes of information out there already. Don't ignore it. Look first, ask questions later.
    I know what overloading a method looks like. But I can't see any value in it (the below example needs four more or less the same functions, which is quite ineffective):

    Code:
    int SomeFunction() { return 0; }
    double SomeFunction() { return 0; }
    string SomeFunction() { return String.Empty; }
    Guid SomeFunction() { return Guid.NewGuid(); }
    Unless I'm not using it right. That's why I asked, how would I go about it

    I have almost 5k posts - I know the rules and I don't ask before I do my search.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: C# generic constraint

    You are, in that case, assuming that I know something that you don't on the subject. Overloading doesn't reduce the amount code. It simply means that have the same method name for the same task for different types. What I'm saying basically is that generics cannot help you here.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: C# generic constraint

    Quote Originally Posted by jmcilhinney View Post
    You are, in that case, assuming that I know something that you don't on the subject. Overloading doesn't reduce the amount code. It simply means that have the same method name for the same task for different types. What I'm saying basically is that generics cannot help you here.
    In that case, thank you for all your time and patience

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width