Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding C# generics and Nullable value type. Return null or nullable

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?

like image 813
Heisenbug Avatar asked Oct 30 '12 16:10

Heisenbug


People also ask

What should I know in this C programming tutorial?

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.

What are the features of C language?

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...

Why should we learn C as the main language?

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.

What is C #programming language?

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.


1 Answers

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.

like image 63
Reed Copsey Avatar answered Oct 14 '22 17:10

Reed Copsey