How to use +, -, *, / etc in C# Generic

  • Thread starter Thread starter My interest
  • Start date Start date
M

My interest

Question is how to make the following class compile?

class foo<T>
{
private T a, b;

public T Sum()
{
return a + b; // this line does not compile.
}
}

The background is that I want this class to be instantiated by all the
"numeric friendly" classes (i.e. those support operator like +,
- ...). These should include both built-in types, (e.g. double) and
user defined class.

Thanks.
 
Question is how to make the following class compile?

class foo<T>
{
private T a, b;

public T Sum()
{
return a + b; // this line does not compile.
}
}

Didn't we just have this thread? Didn't you start that one too?

Hint: just because the operator is now + instead of *, that doesn't make
it a new question.
 
Didn't we just have this thread? Didn't you start that one too?

Hint: just because the operator is now + instead of *, that doesn't make
it a new question.

Sorry, this is a repost. My explorer crashed right after I clicked
the Submit button. So I assumed it failed to post. Sorry about the
re-posting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top