C#: Question referring Generics
-
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 ;-) ).
-
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 ;-) ).
[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
-
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 ;-) ).
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
-
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 ;-) ).
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.
-
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.
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
-
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
thanks for that tip.....I guess a good one ;)