| Inception | 2001 |
| Proprietor | Microsoft |
| Paradigm | Multiple but mainly imperative with features of functional |
| Latest Release | 3.0 |
| Closest to | Java |
Characteristics (similar to Java)
- strongly typed
- single inheritance
- automated memory management (including GC)
- attention to distributed applications, portability, and internationalization
- pointers (as in C) can be used in unsafe blocks
- many compilers use JIT compilation
- supports auto-boxing
- implements a Common Type System where even primitives inherit from
System.Object
Differences from Java
- some keyword differences like
usinginstead ofimportandforeach (string s in my_string)instead offor (String s : myString), etc - partial classes; implementation spread across classes with the same name for code auto-generation purposes and modularization
public partial class MyClass { ... } - difference in parameterized type; the parametric polymorphism is cross-language
- generators
yield return X;public IEnumerable GetEven(IEnumerable numbers) {
foreach (int i in numbers) {
if (i % 2 == 0) yield return i;
}
}
- static classes are ones that cannot be instantiated or inherited from; contain only static members
- member properties
string status = string.Empty;
public string Status {
get { return status; }
protected set { status = value; }
} - nullable vs. non-nullable types (even for primitives) for 1-1 correspondence with SQL data types
int? i = null; - ?? is the null-coalescing operator; it returns the left argument if not null, the right otherwise
return o ?? new Object(); - LINQ is a SQL-like internal query language for operation on collections (as well as XML documents, etc)
int[] array = {1, 5, 2, 10, 7};
// Select squares of all odd numbers in the array sorted in descending order
IEnumerable query = from x in array
where x % 2 == 1
orderby x descending
select x * x;
// Result: 49, 25, 1
- fast member initialization
Customer c = new Customer(); c.Name = "James";
Customer c = new Customer { Name="James" }; - dynamic member assignment
public static class StringExtensions {}
public static string Left(this string s, int n) {
return s.Substring(0, n);
}
string s = "foo";
s.Left(3);