Suppose I have the following class:
public class GenericClass<T>
{
public T Find()
{
//return T if found otherwise null or Nullable<T>
}
}
Somewhere I'd like to specialize my class using T
with a class
, other times with a struct
.
I'm facing this issue: I can't return a Nullable<T>
if T
type isn't restricted to be a struct
.
I would like to provide an implementation of my Find
method that works if T
is specialized with both a class
or a struct
.
In case Find
fails , I'd like to return null
if T
is a class otherwise Nullable<T>
.
Is that possible without using reflection? If yes how?
In this C programming for beginners tutorial, you will learn C programming basics like what is C, variables, loops, strings, classes, functions, pointers, etc. This C programming language tutorial will help you learn all C programming basics. What should I know? Nothing! This C tutorial is an absolute beginner guide to C Programming.
1 'C' was developed by Dennis Ritchie in 1972. 2 It is a robust language. 3 It is a low programming level language close to machine language 4 It is widely used in the software development field. 5 It is a procedure and structure oriented language. 6 It has the full support of various operating systems and hardware platforms. More items...
So, learning ‘C’ as the main language will play an important role while studying other programming languages. It shares the same concepts such as data types, operators, control statements and many more. ‘C’ can be used widely in various applications.
C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg. This tutorial will teach you basic C# programming and will also take you through various advanced concepts related to C# programming language.
You can return default(T)
.
For a class, this will be null
. For any Nullable<T>
, this will be a Nullable<T>
without a value (effectively null
).
That being said, if you use this with a struct
and not a Nullable<T>
as the type, default(T)
will be the default value of the struct.
If you want to make this work uniformly for any class or struct, you would likely need to return two values - you could use the framework as inspiration here, and have a TryXXX method, ie:
public bool TryFind(out T)
You could then use default(T)
when the value isn't found, and return false. This avoids the need for nullable types. You could also write this returning a Tuple<bool, T>
or similar, if you wanted to avoid the out parameter, ie:
public Tuple<bool, T> Find()
A final option, potentially, would be to make your class non-generic, then use a pair of generic methods:
class YourClass // Non generic
{
public T FindReference<T>() where T : class
{
// ...
return null;
}
public Nullable<T> FindValue<T>() where T : struct
{
// ...
return default(T);
}
}
Note that you need distinct names, since you can't have an overloaded method purely based on the return type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With