.net - What is the Difference Between `new object()` and `new {}` in C#? -
first of searched on , found following links on stack overflow:
- is there difference between `new object()` , `new {}` in c#?
- difference between object = new dog() vs dog = new dog()
but i'm not satisfied answer, it's not explained (i didn't well). basically, want know difference between new object()
, new {}
. how, treated @ compile time , runtime?
secondaly, have following code have used webmethods
in asp.net simple application
[webmethod] [scriptmethod(usehttpget = false)] public static object savemenus(menumanager proparams) { object data = new { }; // here im creating instance of 'object' , have typed `new {}` 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 anonymous type data = new { status = true, message = "successfully done!" }; } } catch (exception ex) { data = new { status = false, message = ex.message.tostring() }; } return data; }
so, (as can see in comments in code), how new object(){}
, new {}
differences?
is right way have write code? can suggest best way code?
i know, can't explain , i'm asking alot, that's best have right now.
new {...}
create anonymous object, instance:
object sample = new {}; string samplename = sample.gettype().name; //<- "<>f__anonymoustype0" // not "object"
while new object()
create instance of object
class
object sample = new object() {}; string samplename = sample.gettype().name; // <- "object"
since objects (including anonymous ones) derived object
can type
object sample = new {};
Comments
Post a Comment