Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How are we supposed to implement default(T) in Interfaces?

Put default(T) in an interface. Explicitly implement the interface. The result does not compile

public interface IWhatever<T>
{
   List<T> Foo(T BarObject = default(T));
}

public class ConcreteWhatever: IWhatever<ConcreteWhatever>
{
    List<ConcreteWhatever> Foo(ConcreteWhatever BarObject = default(T)) {}
}

I fully expect default(ConcreteWhatever). What I get is default(T) which results in a compilation error.

I just go in and replace default(T) with null and things are fine. But this is hideous. Why is this happening?

like image 381
P.Brian.Mackey Avatar asked Aug 29 '11 21:08

P.Brian.Mackey


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

You don't have a T in this case, because ConcreteWherever isn't a generic type.

If you want default(ConcreteWhatever) then that's the code you should write.

Are you just complaining about the code auto-generated by Visual Studio? If so, that's a reasonable complaint, but it would be worth being explicit about it... (Note that you're not using explicit interface implementation here - otherwise it would be declared as IWhatever<ConcreteWhatever>.Foo. You don't really have properly implicit implementation either, as otherwise it should be public...)

EDIT: I've just tried the same thing myself, and seen the same result, except the method is made public. Looks like it's just a fault with Visual Studio - I suggest you create a Connect request for it. It's a relatively rare situation though, I suspect - creating a generic interface which specifies an optional parameter which uses the default value of a type parameter as the value...

like image 120
Jon Skeet Avatar answered Oct 13 '22 20:10

Jon Skeet


Shouldn't this line:

List<ConcreteWhatever> Foo(ConcreteWhatever BarObject = default(T)) {}

be:

List<ConcreteWhatever> Foo(ConcreteWhatever BarObject = default(ConcreteWhatever)) {}
like image 2
m.edmondson Avatar answered Oct 13 '22 19:10

m.edmondson