2

Hi I was wondering if something like this was possible or not, I'm sure my title question wasn't clear.

Say I have a function that returns a JSON representation of an object. Note: This is more pseudocode than anything don't correct me on the function.

public static json function(object){

}

Is it possible to define an object like this (I'm working with c#)

var exampleObject = { Name: "x" , PhoneNum: "123456789" }

I was hoping there would be a way to create a object with object attributes just like that on the fly without having to make a class like:

class exampleObject{
string name;
string phoneNum;
}

Does something like this exist? Thanks.

2

2 Answers 2

5

Yes. These are called anonymous types (although format and usage is a little different than what you have there). You can find much more about anonymous types in C# on MSDN: http://msdn.microsoft.com/en-us/library/bb397696.aspx.

An example usage using info from your question would be:

var exampleObject = new { Name = "x", PhoneNum = "123456789" };
Sign up to request clarification or add additional context in comments.

Comments

2

Take a look at ExpandoObject

dynamic exampleObject = new ExpandoObject();      
exampleObject.Name= "X";
exampleObject.PhoneNum= "123456789";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.