Monday, November 2, 2015

10 useful things in C #, which make your life easier

10 useful things in C #, which make your life easier


If you start familiarity with C # or want to expand their knowledge, you should learn these useful features of the language that will help you simplify the code, to avoid mistakes and save a lot of time.

asyns or await

Harness the power of asynchronous programming in order to unlock the possibility of one thread while waiting for the completion of another. For example: if there is a slow web request, then the synchronous programming idle thread will be idle until the web request is made.Asynchronous programming using modifier asyns avoids this. Also useful is the use of asynswith operator await which suspends the thread until the event occurs. For more details you can read here.

Initializers objects, arrays, and collections

It is much easier to create instances of classes, arrays, and collections with special initializers:
The above example can be very useful for unit-testing, but in other situations, the better to create instances using constructors. Details here.

Lambda expressions, delegates and closures

They are needed in a variety of cases, but the biggest benefit they bring when working with LINQ. Learn more about them here.

The operator association with null

?? - Pretty simple, but very useful operator for specific situations. ?? Returns the left operand, if it is not NULL, otherwise it returns the right operand:
Also it can be used with three operands, in which case NULL check will occur from left to right:
And, you can avoid many mistakes when converting NULL into any particular meaning:
For more information about the operator will learn here.

The interpolated string $ "{x}"

New in C # allow you to interpolate 6 (break) line simple and elegant way:
You can substitute different values ​​of the variables in braces in any part of the line that makes the tool very powerful and useful. Read more.

The operator safe navigation

Here is an example of working with the operator:
If either of the operands is equal to NULL, then the value returned will be NULL too. A good way to check the variables in the absence of ads. Details here.

Expression nameof

At first glance, the use nameof is not so very necessary, but in fact nameof very useful when you are using the automatic refactoring tools like ReSharper, and then you need to refer to the argument of the method by name.
But with automatic refactoring is better to implement the same code as follows:
More information here.

Initializers properties

Initializers properties allow you to declare the initial value of the property:
The advantage is that the property values ​​are not fixed, which gives certain flexibility for your class. Also notable is the fact that the properties of initializers completely implemented within a conventional designer.

Operators as and is

The operator is used to determine the identity of the object type, if so, it returns true. For example, if an object is an instance of Person Adult or a type derived from the Adult:
To test the possibility of type conversion using the operator as
If the conversion is not possible, it will be returned to NULL.

Yield keyword

If the keyword yield instructions, this means that the method, the operator or the access method get in which it occurs is an iterator. In the following example, the method will return the order of the various powers of two exponentially: 2, 4, 8, 16, 32, 64, 128, 256.
Keyword yield avoids creating additional explicit class, which contains the status of the transfer. A great and easy way to create a sequence of objects. For more details read here.

By Unknown with No comments

0 коммент.:

Post a Comment