What is the Difference Between `new object()` and `new {}` in C#?
.net, c#, object
Solution
`new {...}` always creates an anonymous object, for instance:
Object sample = new {};
String sampleName = sample.GetType().Name; // <- something like "<>f__AnonymousType0"
// not "Object"
while `new Object()` creates an instance of `Object` class
Object sample = new Object() {};
String sampleName = sample.GetType().Name; // <- "Object"
since all objects (including anonymous ones) are derived from `Object` you can always type
Object sample = new {};
Problem
First of all i searched on this and i found the following links on Stack Overflow: - Is there any difference between `new object()` and `new {}` in c#? - Difference between object a = new Dog() vs Dog a = new Dog() But i'm not satisfied with this answer, it's not explained well (i didn't get it well). Basically, i want to know the difference between `new object()` and `new {}`. How, they are treated at compile time and runtime? Secondaly, i have the following code which i have used for `WebMethods` in my asp.net simple application ``` [WebMethod] [ScriptMethod(UseHttpGet = false)] public static object SaveMenus(MenuManager proParams) { object data = new { }; // here im creating an instance of an 'object' and i have typed it `new {}` but not `new object(){}`. try { MenuManager menu = new MenuManager(); menu.Name = proParams.Name; menu.Icon = proParams.Icon; bool status = menu.MenuSave(menu); if (status) { // however, here i'm returning an anonymous type data = new { status = true, message = "Successfully Done!" }; } } catch (Exception ex) { data = new { status = false, message = ex.Message.ToString() }; } return data; } ``` So, (as you can see in comments in code), How `new object(){}` and `new {}` differences? Is this even the right way that i have write the code? Can you suggest a best way for this code? I know, i can't explain it well and i'm asking alot, but that's the best i have right now.