Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create an object without a class in C#?

Tags:

c#

object

In many languages you can create an object without creating a data type, and add properties to that object.

For example in JS or AS:

 var myObject = {};  myObject.myParameter = "hello world"; 

Or you can create structures in C and C++.

Is it possible to do that in C#?

like image 543
Pier Avatar asked Oct 21 '12 18:10

Pier


People also ask

Can we create object without 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.

Can we create object without class in C#?

We can Create objects in C# in the following ways: 1) Using the 'new' operator: A class is a reference type and at the run time, any object of the reference type is assigned a null value unless it is declared using the new operator.

Can object be created in C?

Using the new keyword in java is the most basic way to create an object. This is the most common way to create an object in java. Almost 99% of objects are created in this way. By using this method we can call any constructor we want to call (no argument or parameterized constructors).

Is it always necessary to create objects from class in C ++?

C++ Objects. When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, we need to create objects.


1 Answers

Anonymous Types is what you looking for. Eg -

var v = new { Amount = 108, Message = "Hello" }; 

Above code will create a new object with properties Amount and Message.

like image 156
Rohit Vats Avatar answered Sep 30 '22 13:09

Rohit Vats