0

I was working on javascript before.

In javascript, I do this to create dynamic object and assign properties to it.

var house = new object();
house.kitchen = 1;
house.bedroom = 2;
house.livingroom =3;

How to do the same stuff in C#?

Powered
  • 43
  • 5
  • This question is a bit too basic to fit the StackOverflow format. It's probably a good idea to search for a basic C# tutorial and come back if you have any specific questions. To start you off, you want to create a `class`, and use this to create an object with `new ClassName()`. – TheEvilPenguin Oct 31 '13 at 02:39
  • 2
    Exacly like that. Not a single change needed, except for `new house()`. In all seriousness, please go read a basic tutorial. We're here to help with problems and explain them, not provide a step by step tutorial of a language. – Jeroen Vannevel Oct 31 '13 at 02:39
  • If you are already asking how to create objects then read this first http://msdn.microsoft.com/en-us/library/618ayhy6%28v=vs.71%29.aspx – Robert Hoffmann Oct 31 '13 at 02:45
  • Aside the dynamic object, c# objects should be considered the equivalent of Classes ..yes we call them objects because at the base everything in c# is an object but in reality we do new Class() – Robert Hoffmann Oct 31 '13 at 02:51
  • 1
    @Robert That isn't quite right. An object is an instance of a class - a class was used to create it and a class defines its structure and behaviour, but it isn't a class. – TheEvilPenguin Oct 31 '13 at 03:10
  • Yeah i know, but in js you can go defining stuff inline like var house = {}; and house.kitchen = 1; whereas in c# you first have to define your object (class) before you can call new() on it, this is similar to creating a function and calling new() on it, it becomes a class in js. Ok we also have anonymous functions in c# like var house = new { kitchen = 1 }; ..but now i think he's gonna be confused -:) – Robert Hoffmann Oct 31 '13 at 10:11
  • Hi All, I have a basic idea of C# and I did some work on it in the past. But in recent times, I very much concentrated on J Script and now I confuse and use same concepts in C#. Anyway I got answer from this thread, I was looking for the same way as J Script to create dynamic object in C#. So I need to use `dynamic house = new ExpandoObject();` to create dynamic objects in C#. Thanks to every one and very special thanks to Rodrick Chapman. – Powered Oct 31 '13 at 17:26
  • You could always use a dictionary like this, which makes it behave pretty similarly to Javascript. You can't say `house.kitchen`, but you can say `house["kitchen"]`, which is also true in Javascript. I know it's not the same as defining a class and all that, but it's handy if you're quickly storing values that are just going to be converted to Json and passed back to an Ajax call. `Dictionary house = new Dictionary(); house["kitchen"] = 1; house["bedroom"] = 2; house["livingroom"] = 3;` – James Toomey Apr 15 '16 at 20:29

2 Answers2

5

in C# there is something called dynamic that you could use

dynamic house = new ExpandoObject();
house.Kitchen = 1;
house.Bedroom = 2;
house.Livingroom =3;

but if I am new to c# that is not were I would start, dynamic has very specific uses, typically in more advanced situations. What you really should be looking at is Classes and objects. C# is a static language, and dynamic is there just to give it a bit more flexibility.

Bassam Mehanni
  • 14,796
  • 2
  • 33
  • 41
  • 2
    +1. `var house = (dynamic)new object()` is another option if one likes `var` or even `var house = new {kitchen = 1, bedroom = 2, livingroom =3 };` (but as you've pointed out better to use strongly typed object). – Alexei Levenkov Oct 31 '13 at 02:54
  • 1
    -1 This code doesn't do what's advertised. It will compile but once you try to set one of properties like that you'll get a `RuntimeBinderException`. – Rodrick Chapman Oct 31 '13 at 03:05
  • @RodrickChapman good point, I was being to careless with the code sample, my main point was not really about dynamic but rather about the static nature of C# verses javascript. But still, good catch. Thanks! – Bassam Mehanni Oct 31 '13 at 18:20
0

I'd guess that you're looking for something like the ExpandoObject in C#. See this question for details on how to use it. Basically, it allows you to add properties to objects at runtime in a manner similar to Javascript. Please note, however, that idiomatic C# doesn't make much use of it.

The analogous C# code for your JavaScript example would be something like this:

dynamic house = new ExpandoObject();
house.kitchen = 1;
house.bedroom = 2;
house.livingroom = 3;
Community
  • 1
  • 1
Rodrick Chapman
  • 5,437
  • 2
  • 31
  • 32
  • Will this dynamic work in visual studio background?? – Powered Oct 31 '13 at 02:50
  • Is there any special library I need to include in visual studio-08 to use this dynamic feature? Right now it couldn't identify dynamic. – Powered Oct 31 '13 at 17:32
  • @Powered - `Dynamic` is a C# 4.0 feature which shipped with Visual Studio 2010. The latest version of the Express Edition of Visual C#, which you can get for free, does include that feature. – Rodrick Chapman Oct 31 '13 at 20:33