0

I wanted to create an anonymous C# object with no class definition. Similar to javascript object. Sample code not working:

var data = new
  {
    groups = new object[] {
      new {
        header = "Facebook",
        buttons = new object[] {
          new {
            label = "Create new post"
          }
        }
      }
    }
  };
5
  • 1
    And what´s the question? Commented Jul 22, 2015 at 12:12
  • 1
    I was about to ask and found it out myself. So just wanted to share the solution. Commented Jul 22, 2015 at 12:41
  • 1
    Than you should either delete your question or write it in a manner that helps the community. But with your current question there is no chance to help ypu out on this topic. Since the problem was a simple typo I vote for closing this question. Commented Jul 22, 2015 at 12:45
  • 1
    For me it is more of a conceptual mistake, not a typo. I actually thought this was the correct way Commented Jul 22, 2015 at 12:47
  • It actually helped me. I found it by googling adhoc object c# and got this question. I'm rewriting it to confirm with SO guidelines. Commented Dec 12, 2020 at 15:01

1 Answer 1

6

The problem was the new object[]. It is just new []. Working code:

var data = new
  {
    groups = new [] {
      new {
        header = "Facebook",
        buttons = new [] {
          new {
            label = "Create new post"
          }
        }
      }
    }
  };
Sign up to request clarification or add additional context in comments.

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.