Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C#: Question referring Generics

C#: Question referring Generics

Scheduled Pinned Locked Moved C#
questioncsharptutorial
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SJ_Phoenix
    wrote on last edited by
    #1

    if I have a generic class like this for example: public class GenericExample { public GenericExample(T parameter) {} //... } when I instantiate it then: GenericExample ge1 = new GenericExample(String.Empty); GenericExample ge2 = new GenericExample(null); so here is my question: how can I verify, whether parameter is null, or whether it is string.Empty, although I don't know, if T is an value-Type or reference-type. (yes...I know string is a reference type but I had no other example in mind ;-) ).

    H S 3 Replies Last reply
    0
    • S SJ_Phoenix

      if I have a generic class like this for example: public class GenericExample { public GenericExample(T parameter) {} //... } when I instantiate it then: GenericExample ge1 = new GenericExample(String.Empty); GenericExample ge2 = new GenericExample(null); so here is my question: how can I verify, whether parameter is null, or whether it is string.Empty, although I don't know, if T is an value-Type or reference-type. (yes...I know string is a reference type but I had no other example in mind ;-) ).

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      [EDIT: Note, this was posted while the < and > brackets were not appearing in the root thread. I leave it here for posterity.] This is not a template class, which would look like:

      public class GenericExmaple<T>
      {
      public GenericExample(T parameter) {}
      // ...
      }

      And would be declared as a variable like so:

      GenericExample<string> ge = new GenericExample<string>(null);

      If you want a class that takes a "generic" parameter, use object:

      public class GenericExample
      {
      public GenericExample(object parameter)
      {
      if (parameter == null) throw new ArgumentNullException("parameter");
      }
      }

      When you define the parameter as an object, even if it's a value type it will be boxed as an object and can be compared to null, although it will never be null since value types can't be null. A reference could and thus is subject to the exception if it is null.

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      • S SJ_Phoenix

        if I have a generic class like this for example: public class GenericExample { public GenericExample(T parameter) {} //... } when I instantiate it then: GenericExample ge1 = new GenericExample(String.Empty); GenericExample ge2 = new GenericExample(null); so here is my question: how can I verify, whether parameter is null, or whether it is string.Empty, although I don't know, if T is an value-Type or reference-type. (yes...I know string is a reference type but I had no other example in mind ;-) ).

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        I see you fixed your code, which changes things a little bit... You can always use conditions like so to check if it's null or empty in your constructor regardless of the type (so long as it's a reference type):

        if (parameter == null) throw new ArgumentNullException("parameter");
        if (parameter.Equals(string.Empty))
        throw new ArgumentException("Error", "parameter");

        If you want value types to also be valid for your generic class, then you can use something like this in your constructor:

        Type t = typeof(T);
        if (!t.IsValueType)
        {
        // Use conditions from first example
        }

        Microsoft MVP, Visual C# My Articles

        1 Reply Last reply
        0
        • S SJ_Phoenix

          if I have a generic class like this for example: public class GenericExample { public GenericExample(T parameter) {} //... } when I instantiate it then: GenericExample ge1 = new GenericExample(String.Empty); GenericExample ge2 = new GenericExample(null); so here is my question: how can I verify, whether parameter is null, or whether it is string.Empty, although I don't know, if T is an value-Type or reference-type. (yes...I know string is a reference type but I had no other example in mind ;-) ).

          S Offline
          S Offline
          SJ_Phoenix
          wrote on last edited by
          #4

          thanks, Heath. the brackets '<' and '>' were missing because I forgot to check the option 'Don't treat <'s as html-tags' and recognized just later that the brackets were not shown.

          H 1 Reply Last reply
          0
          • S SJ_Phoenix

            thanks, Heath. the brackets '<' and '>' were missing because I forgot to check the option 'Don't treat <'s as html-tags' and recognized just later that the brackets were not shown.

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            It also helps to put code between <pre></pre> tags, which usually translates the brackets automatically (though not always). This also makes for a nicer fixed-width format (including spaces and tabs you may use) for display code in the forums (and in articles, for that matter).

            Microsoft MVP, Visual C# My Articles

            S 1 Reply Last reply
            0
            • H Heath Stewart

              It also helps to put code between <pre></pre> tags, which usually translates the brackets automatically (though not always). This also makes for a nicer fixed-width format (including spaces and tabs you may use) for display code in the forums (and in articles, for that matter).

              Microsoft MVP, Visual C# My Articles

              S Offline
              S Offline
              SJ_Phoenix
              wrote on last edited by
              #6

              thanks for that tip.....I guess a good one ;)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups