For a seasoned software developer, knowing just the basics and theories of programming is going to do any good. They must also be aware of little tricks which makes development easy and fun. In this article I'll share few tips which we can use to improve our coding. (I'll be focusing on C# here.)
Null-Coalescing operator
Rather than explaining its better to show it using an example.You can also chain these
Stackoverflow :What do ?? means in C#
yield method
using yield you can return each element one at a time for a list. (Lazy) (MSDN, SO, CodeProject easy to understand)
IEnumerable<int> Integers()
{
List<int> list = new List<int> { 1, 2, 3 };
foreach (var item in list)
{
yield return item;
}
}
Extension methods
Allows you to add methods to existing types without creating new derived type. These are special kind of static method. The most common extension methods are LINQ standard query operators that add query functionality to existing System.Collections.IEnumberable and System.Collections.Generic.IEnumerable<T> types.
You can use extension methods to extend a class or interface, but not to override them. An extension method with same name and signature as an interface or class method will never be called. At compile time extension methods always have low priority than instance methods.
Defining extension method on Object will make the method available to all objects. This won't have any performance cost. Basically it won't get attached to each object. Rather you can call the extension method from any object. (See SO article)
Defining extension method on Object will make the method available to all objects. This won't have any performance cost. Basically it won't get attached to each object. Rather you can call the extension method from any object. (See SO article)
I'll update this list continually. Hope this list will be useful for you.
P.S: Find out more Hidden features of C# (SO)
image credit : blastmobilefitness
0 comments:
Post a Comment