In php we can create standard classes and objes like this
< ?php
$car = new stdClass;
$car->Colour = 'black';
$car->type = 'sports';print_r($car);
?>
is there any similar way in C#.net ??? I'm very new to C# anyone can help???
Object-Oriented Programming in C Although the fundamental OOP concepts have been traditionally associated with object-oriented languages, such as Smalltalk, C++, or Java, you can implement them in almost any programming language including portable, standard-compliant C (ISO-C90 Standard).
This document describes the simplest possible coding style for making classes in C. It will describe constructors, instance variables, instance methods, class variables, class methods, inheritance, polymorphism, namespaces with aliasing and put it all together in an example project.
The main purpose of Objective-C programming language is to add object orientation to the C programming language and classes are the central feature of Objective-C that support object-oriented programming and are often called user-defined types.
You could use Anonymous Types in C#, e.g.: taken from the link:
var v = new { Amount = 108, Message = "Hello" };
Console.WriteLine(v.Amount + v.Message);
Note the Remarks section for their limitations (e.g. can only be cast to object) etc.
C# is statically typed, so regular classes must be declared before they can be used. However, anonymous types allow you to declare classes based on initialization. Once declared neither the type nor the instance can be changed though.
For a more dynamic approach take a look at ExpandoObject which allows dynamically adding properties. This requires C# 4 and the reference must be declared as dynamic.
dynamic car = new ExpandoObject();
car.Color = "Black";
car.Type "Sports";
You can use anonymous types, but they aren't quite the same: http://msdn.microsoft.com/en-us/library/bb397696.aspx
C# is really all about strong-typing, so weak-type support is, well, pretty weak. Depending on what you're actually doing here, an anonymous type might work, but in general you should create real classes when coding in C#. For your Car
example above, I would usually create a class. It's more code, but that's the way C# is. PHP you might create an app with, say, 10 PHP files, but the same C# app will probably end up being 20-30.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With