2

I'm trying to implement a class that stores a generic nullable type:

public class myclass<T?>
{
    T? myvariable;

    public T? myfunction(){
        return myvariable;
    }
 }

And while the above class compiles just fine, the actual use that provides trouble:

myclass<int?> c = new myclass<int>();
int? = c.myfunction();   // does not work: implicit cast from type T? in int? not possible.
// or, assuming the variable is not null
int = c.myfunction().Value; // implicit cast from type T in int not possible.

What am I doing wrong or how can I work around this?

2
  • And your example doesn't make sense. What are you trying to achieve? Commented Aug 1, 2010 at 17:12
  • Indeed it does not compile ... sorry, an other error must have covered it in the syntax checker. What I am tying to achive is a class that stores a nullable type. (What is missing from the above example is the part where the variable is set) Commented Aug 1, 2010 at 17:16

1 Answer 1

5

Neither of the examples compile:

The rest of your second example should compile OK, however.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.