1.0
C# 1.0, released in 2002 as part of Microsoft's .NET initiative, introduced a wide range of features designed to combine the power and flexibility of C++ with the simplicity of Visual Basic. Here are some of the key features of C# 1.0 along with examples for each:
1. Strong Typing
C# is a strongly-typed language, meaning that every variable and object instance must have a declared type.
2. Object-Oriented
Everything in C# is associated with classes and objects, along with their attributes and methods.
class Person {
public string Name { get; set; }
public void Introduce() {
Console.WriteLine($"Hi, my name is {Name}");
}
}
Person person = new Person();
person.Name = "John";
person.Introduce(); // Output: "Hi, my name is John"
3. Automatic Memory Management
C# uses garbage collection to automatically manage memory, eliminating the need to manually release memory.
// Memory allocation
Person person = new Person();
// Memory is automatically reclaimed by the garbage collector when 'person' is no longer in use.
4. Properties
Properties provide a flexible mechanism to read, write, or compute the values of private fields.
class Person {
private string name;
public string Name {
get { return name; }
set { name = value; }
}
}
5. Events and Delegates
C# supports events and delegates, enabling a way to provide event handling.
public delegate void EventHandler(string message);
class EventDemo {
public event EventHandler OnChange;
public void Change(string message) {
if (OnChange != null) {
OnChange(message);
}
}
}
EventDemo demo = new EventDemo();
demo.OnChange += (message) => Console.WriteLine(message);
demo.Change("Event triggered"); // Output: "Event triggered"
6. Versioning
C# includes features such as assemblies and strong names to provide versioning control.
// Assemblies can be given strong names to ensure that applications reference the correct version of a library.
7. Error Handling with Exceptions
C# uses structured exception handling, based on try, catch, and finally blocks.
try {
// Code that might throw an exception
int[] numbers = {1, 2, 3};
Console.WriteLine(numbers[3]); // This will throw an IndexOutOfRangeException
} catch (IndexOutOfRangeException e) {
Console.WriteLine("An exception occurred: " + e.Message);
} finally {
// Code to execute after try/catch, regardless of an exception
Console.WriteLine("Finally block executed");
}
8. Array
Arrays are used to store multiple values in a single variable.