I created a GitHub project – brunolm/csharp-features to show what is coming and how to use it.
Extension methods are methods that can be created extending existing types without the need to inherit from a class and creating your own custom logic. It can also be applied on interfaces.
Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
That means we can extend methods from any type, even types where we don’t have access to the source code.
Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.
We build extension methods by declaring it as a static method, but there is a little tweak makes it compile into a method that can be called from an instance.
Reflection uses the object of type
Type that describes assemblies, modules, types…
Through reflection it is possible to do many different things, for example:
- Dynamically create object instances
- Dynamically invoke methods
- Check if an object implements an interface
- Manipulate attributes set on objects
- Mock values for testing
- Retrieve debug information
These are just a few things you can do with it. In this post I’m going to show a simple example on how to use it to get descriptions on enums.
Linq is a powerful feature introduced on .NET 3.5 that allows you to easily query your objects to manipulate data.
Entity Framework uses Linq queries to generate database queries and retrieve the data for you. So you don’t have to worry about writing SQL anymore.
Linq also provides methods to work with XML among other things.
Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.
Managed Extensibility Framework (MEF) is a built-in set of elements that allows you to “export” and “import” objects across projects which allows you not to rely on hard dependencies.
The Managed Extensibility Framework or MEF is a library for creating lightweight, extensible applications. It allows application developers to discover and use extensions with no configuration required. It also lets extension developers easily encapsulate code and avoid fragile hard dependencies. MEF not only allows extensions to be reused within applications, but across applications as well.
The .NET Framework provides us a set of attributes that we can use to validate objects. By using the namespace
System.ComponentModel.DataAnnotations we can annotate our model’s properties with validation attributes. Continue reading