There are some unique features of C# that make itself stand out from the sheer amount of programming languages in the wild.
Support for
- Property syntax
- Partial classes and methods
- Extension methods
- Anonymous types
- Language Integrated Query (LINQ)
are some of the prominent ones.
Property syntax
Property syntax allow classes to provide a flexible mechanism for getting, setting, or computing the value of a private field (Docs, 2017).
public class SaleItem
{
public string Name { get; set; }
public decimal Price { get; set; }
}
Partial classes and methods
Partial classes and methods allow a class’s members to be defined in multiple files (Docs, 2015) which provide the following benefits:
- multiple programmers to work on the same class at the same time
- provide a mechanism to extend a compiler generated code
// File1
public partial class Employee
{
public void DoWork()
{
}
// partial method
partial void onNameChanged();
}
// File2
public partial class Employee
{
public void GoToLunch()
{
}
partial void onNameChanged()
{
// method body
}
}
Extension methods
Extension method is a statically-defined method that also allows programmers to extend the functionality of a class that cannot be modified, such as the built-in types.
public static class StringExtension
{
// extend the built in string type
public static int WordCount(this string str)
{
return str.Split(new char[] {' ', '.','?'}, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
Anonymous types
Anonymous type provide a simpler way to encapsulate a set of read only property into a single object and it is not necessary to define a type first. The type name is generated by compiler (Docs, 2015).
var v = new { Amount = 108, Message = "Hello" };
Console.WriteLine(v.Amount + v.Message);
Language Integrated Query (LINQ)
Language Integrated Query (LINQ) provide a unified language for querying data from various data source including XML documents, SQL databases, ADO.NET datasets, .NET collections or any other format provided that a LINQ provider is available (Docs, 2015)
// Query syntax
var result = from student in students
where student.Scores[0] > 90
select student;
// Fluent syntax
var result = students.Where(student => student.Scores[0] > 90);
References
- Docs, M. (2017, March 9). Properties (C# Programming Guide). Retrieved from https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
- Docs, M. (2015, July 19). Partial Classes and Methods (C# Programming Guide). Retrieved from https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods
- Docs, M. (2015, July 19). Anonymous Types (C# Programming Guide). Retrieved from https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types
- Docs, M. (2015, July 19). Introduction to LINQ Queries (C#). Retrieved from https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/introduction-to-linq-queries