Null checking a generic type [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












Assuming I have a method with this signature



public void DoStuffWith<T>(T foo)


and I want to allow both structs and classes to be passed as T, I was wondering about how to best do the null-checking in case T is a class.



I came up with following two approaches:




1. Checking whether T is a struct or a class at runtime:



public void DoStuffWith<T>(T foo)

if(!typeof(T).IsValueType && foo == null)

throw new ArgumentNullException(nameof(foo));


...



2. Having two overloads with generic type constraints:



public void DoStuffWithStruct<T>(T foo) where T : struct

...


public void DoStuffWithClass<T>(T foo) where T : class

if(foo == null)

throw new ArgumentNullException(nameof(foo));


...




However, I dislike both approaches. I don't like the runtime type-checking at #1 and I don't like having multiple, differently named overloads at #2.



Which of the approaches do you suggest or do you think I should maybe consider a totally different approach altogether?



I appreciate any feedback.







share|improve this question











closed as off-topic by Dannnno, Denis, Sam Onela, Stephen Rauch, Malachi♦ May 8 at 22:25


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Dannnno, Denis, Sam Onela, Stephen Rauch, Malachi
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR?
    – Dannnno
    May 8 at 19:59










  • @Dannnno thank you for the note. I am sorry I created an off-topic question... since there has been an answer already, I cannot delete the question anymore. What do you recommend now?
    – Thomas Flinkow
    May 8 at 20:01






  • 1




    At this point no need to do anything. If you have an actual code sample feel free to add that (but don't remove anything as that would invalidate the answer) for additional context.
    – Dannnno
    May 8 at 20:04










  • @Dannnno as I wanted to add the actual code, I discovered that I made a lapse of thought. Therefore, the whole question is irrelevant and I will try to VTC it.
    – Thomas Flinkow
    May 8 at 20:09
















up vote
0
down vote

favorite












Assuming I have a method with this signature



public void DoStuffWith<T>(T foo)


and I want to allow both structs and classes to be passed as T, I was wondering about how to best do the null-checking in case T is a class.



I came up with following two approaches:




1. Checking whether T is a struct or a class at runtime:



public void DoStuffWith<T>(T foo)

if(!typeof(T).IsValueType && foo == null)

throw new ArgumentNullException(nameof(foo));


...



2. Having two overloads with generic type constraints:



public void DoStuffWithStruct<T>(T foo) where T : struct

...


public void DoStuffWithClass<T>(T foo) where T : class

if(foo == null)

throw new ArgumentNullException(nameof(foo));


...




However, I dislike both approaches. I don't like the runtime type-checking at #1 and I don't like having multiple, differently named overloads at #2.



Which of the approaches do you suggest or do you think I should maybe consider a totally different approach altogether?



I appreciate any feedback.







share|improve this question











closed as off-topic by Dannnno, Denis, Sam Onela, Stephen Rauch, Malachi♦ May 8 at 22:25


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Dannnno, Denis, Sam Onela, Stephen Rauch, Malachi
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR?
    – Dannnno
    May 8 at 19:59










  • @Dannnno thank you for the note. I am sorry I created an off-topic question... since there has been an answer already, I cannot delete the question anymore. What do you recommend now?
    – Thomas Flinkow
    May 8 at 20:01






  • 1




    At this point no need to do anything. If you have an actual code sample feel free to add that (but don't remove anything as that would invalidate the answer) for additional context.
    – Dannnno
    May 8 at 20:04










  • @Dannnno as I wanted to add the actual code, I discovered that I made a lapse of thought. Therefore, the whole question is irrelevant and I will try to VTC it.
    – Thomas Flinkow
    May 8 at 20:09












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Assuming I have a method with this signature



public void DoStuffWith<T>(T foo)


and I want to allow both structs and classes to be passed as T, I was wondering about how to best do the null-checking in case T is a class.



I came up with following two approaches:




1. Checking whether T is a struct or a class at runtime:



public void DoStuffWith<T>(T foo)

if(!typeof(T).IsValueType && foo == null)

throw new ArgumentNullException(nameof(foo));


...



2. Having two overloads with generic type constraints:



public void DoStuffWithStruct<T>(T foo) where T : struct

...


public void DoStuffWithClass<T>(T foo) where T : class

if(foo == null)

throw new ArgumentNullException(nameof(foo));


...




However, I dislike both approaches. I don't like the runtime type-checking at #1 and I don't like having multiple, differently named overloads at #2.



Which of the approaches do you suggest or do you think I should maybe consider a totally different approach altogether?



I appreciate any feedback.







share|improve this question











Assuming I have a method with this signature



public void DoStuffWith<T>(T foo)


and I want to allow both structs and classes to be passed as T, I was wondering about how to best do the null-checking in case T is a class.



I came up with following two approaches:




1. Checking whether T is a struct or a class at runtime:



public void DoStuffWith<T>(T foo)

if(!typeof(T).IsValueType && foo == null)

throw new ArgumentNullException(nameof(foo));


...



2. Having two overloads with generic type constraints:



public void DoStuffWithStruct<T>(T foo) where T : struct

...


public void DoStuffWithClass<T>(T foo) where T : class

if(foo == null)

throw new ArgumentNullException(nameof(foo));


...




However, I dislike both approaches. I don't like the runtime type-checking at #1 and I don't like having multiple, differently named overloads at #2.



Which of the approaches do you suggest or do you think I should maybe consider a totally different approach altogether?



I appreciate any feedback.









share|improve this question










share|improve this question




share|improve this question









asked May 8 at 19:53









Thomas Flinkow

1407




1407




closed as off-topic by Dannnno, Denis, Sam Onela, Stephen Rauch, Malachi♦ May 8 at 22:25


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Dannnno, Denis, Sam Onela, Stephen Rauch, Malachi
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Dannnno, Denis, Sam Onela, Stephen Rauch, Malachi♦ May 8 at 22:25


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Dannnno, Denis, Sam Onela, Stephen Rauch, Malachi
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 3




    I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR?
    – Dannnno
    May 8 at 19:59










  • @Dannnno thank you for the note. I am sorry I created an off-topic question... since there has been an answer already, I cannot delete the question anymore. What do you recommend now?
    – Thomas Flinkow
    May 8 at 20:01






  • 1




    At this point no need to do anything. If you have an actual code sample feel free to add that (but don't remove anything as that would invalidate the answer) for additional context.
    – Dannnno
    May 8 at 20:04










  • @Dannnno as I wanted to add the actual code, I discovered that I made a lapse of thought. Therefore, the whole question is irrelevant and I will try to VTC it.
    – Thomas Flinkow
    May 8 at 20:09












  • 3




    I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR?
    – Dannnno
    May 8 at 19:59










  • @Dannnno thank you for the note. I am sorry I created an off-topic question... since there has been an answer already, I cannot delete the question anymore. What do you recommend now?
    – Thomas Flinkow
    May 8 at 20:01






  • 1




    At this point no need to do anything. If you have an actual code sample feel free to add that (but don't remove anything as that would invalidate the answer) for additional context.
    – Dannnno
    May 8 at 20:04










  • @Dannnno as I wanted to add the actual code, I discovered that I made a lapse of thought. Therefore, the whole question is irrelevant and I will try to VTC it.
    – Thomas Flinkow
    May 8 at 20:09







3




3




I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR?
– Dannnno
May 8 at 19:59




I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR?
– Dannnno
May 8 at 19:59












@Dannnno thank you for the note. I am sorry I created an off-topic question... since there has been an answer already, I cannot delete the question anymore. What do you recommend now?
– Thomas Flinkow
May 8 at 20:01




@Dannnno thank you for the note. I am sorry I created an off-topic question... since there has been an answer already, I cannot delete the question anymore. What do you recommend now?
– Thomas Flinkow
May 8 at 20:01




1




1




At this point no need to do anything. If you have an actual code sample feel free to add that (but don't remove anything as that would invalidate the answer) for additional context.
– Dannnno
May 8 at 20:04




At this point no need to do anything. If you have an actual code sample feel free to add that (but don't remove anything as that would invalidate the answer) for additional context.
– Dannnno
May 8 at 20:04












@Dannnno as I wanted to add the actual code, I discovered that I made a lapse of thought. Therefore, the whole question is irrelevant and I will try to VTC it.
– Thomas Flinkow
May 8 at 20:09




@Dannnno as I wanted to add the actual code, I discovered that I made a lapse of thought. Therefore, the whole question is irrelevant and I will try to VTC it.
– Thomas Flinkow
May 8 at 20:09










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










If you try the following :



class Program

static void Main(string args)

Foo<string>(null);
Foo<string>("asd");
Foo<int>(1);
Console.ReadKey();


public static void Foo<T>(T test)

if(test == null)

Console.WriteLine("ho ho it's null");
return;


Console.WriteLine(test.GetType());




You will realize that both the options are, excuse me, useless. The language already supports this behavior and if T is a struct, test == null will simply return false all the time.






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    If you try the following :



    class Program

    static void Main(string args)

    Foo<string>(null);
    Foo<string>("asd");
    Foo<int>(1);
    Console.ReadKey();


    public static void Foo<T>(T test)

    if(test == null)

    Console.WriteLine("ho ho it's null");
    return;


    Console.WriteLine(test.GetType());




    You will realize that both the options are, excuse me, useless. The language already supports this behavior and if T is a struct, test == null will simply return false all the time.






    share|improve this answer

























      up vote
      3
      down vote



      accepted










      If you try the following :



      class Program

      static void Main(string args)

      Foo<string>(null);
      Foo<string>("asd");
      Foo<int>(1);
      Console.ReadKey();


      public static void Foo<T>(T test)

      if(test == null)

      Console.WriteLine("ho ho it's null");
      return;


      Console.WriteLine(test.GetType());




      You will realize that both the options are, excuse me, useless. The language already supports this behavior and if T is a struct, test == null will simply return false all the time.






      share|improve this answer























        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        If you try the following :



        class Program

        static void Main(string args)

        Foo<string>(null);
        Foo<string>("asd");
        Foo<int>(1);
        Console.ReadKey();


        public static void Foo<T>(T test)

        if(test == null)

        Console.WriteLine("ho ho it's null");
        return;


        Console.WriteLine(test.GetType());




        You will realize that both the options are, excuse me, useless. The language already supports this behavior and if T is a struct, test == null will simply return false all the time.






        share|improve this answer













        If you try the following :



        class Program

        static void Main(string args)

        Foo<string>(null);
        Foo<string>("asd");
        Foo<int>(1);
        Console.ReadKey();


        public static void Foo<T>(T test)

        if(test == null)

        Console.WriteLine("ho ho it's null");
        return;


        Console.WriteLine(test.GetType());




        You will realize that both the options are, excuse me, useless. The language already supports this behavior and if T is a struct, test == null will simply return false all the time.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 8 at 19:58









        IEatBagels

        8,50623077




        8,50623077












            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?