Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a inline object with inline variables without a parent class

Tags:

c#

I am trying to do something like:

Object [] x = new Object[2];  x[0]=new Object(){firstName="john";lastName="walter"}; x[1]=new Object(){brand="BMW"}; 

I want to know if there is a way to achieve that inline declaration in C#

like image 235
deadlock Avatar asked Jan 17 '11 01:01

deadlock


People also ask

Can you have an object without a class?

We can create an object without creating a class in PHP, typecasting a type into an object using the object data type. We can typecast an array into a stdClass object. The object keyword is wrapped around with parenthesis right before the array typecasts the array into the object.

What is inline variable declaration?

The new inline variable declaration syntax allows you to declare the variable directly in a code block (allowing also multiple symbols as usual): procedure Test; begin var I: Integer; I := 22; ShowMessage (I. ToString); end; procedure Test2; begin var I, K: Integer; I := 22; K := I + 10; ShowMessage (K.


1 Answers

yes, there is:

object[] x = new object[2];  x[0] = new { firstName = "john", lastName = "walter" }; x[1] = new { brand = "BMW" }; 

you were practically there, just the declaration of the anonymous types was a little off.

like image 129
hunter Avatar answered Sep 19 '22 08:09

hunter