Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between `new object()` and `new {}` in c#?

in c#,

var x = new {};

declares an anonymous type with no properties. Is this any different from

var x = new object();

?

like image 951
Gabe Moothart Avatar asked Jun 22 '09 15:06

Gabe Moothart


1 Answers

Yes, the types used are different. You can tell this at compile-time:

var x = new {};
// Won't compile - no implicit conversion from object to the anonymous type
x = new object(); 

If you're asking whether new{} is ever useful - well, that's a different matter... I can't immediately think of any sensible uses for it.

like image 113
Jon Skeet Avatar answered Oct 18 '22 15:10

Jon Skeet