Posts

Showing posts from November, 2016

Lession 58: C# Object Initializer Syntax

C# 3.0 (.NET 3.5) introduced  Object Initializer Syntax , a new way to initialize an object of a class or collection. Object initializers allow you to assign values to the fields or properties at the time of creating an object without invoking a constructor. Consider the following example.

Lession 57: C# Dynamic type

Image
C# 4.0 (.NET 4.5) introduced a new type that avoids compile time type checking. You have learned about the implicitly typed variable-  var  in the previous section where the compiler assigns a specific type based on the value of the expression. A dynamic type escapes type checking at compile time; instead, it resolves type at run time.

Lession 56: C# Anonymous Type

Image
Anonymous type, as the name suggests, is a type that doesn't have any name. C# allows you to create an object with the  new  keyword without defining its class. The  implicitly typed variable- var  is used to hold the reference of anonymous types.

Lession 55: C# Extension Method

Image
Extension methods, as the name suggests, are additional methods. Extension methods allow you to inject additional methods without modifying, deriving or recompiling the original class, struct or interface. Extension methods can be added to your own custom class, .NET framework classes, or third party classes or interfaces. In the following example, IsGreaterThan() is an extension method for int type, which returns true if the value of the int variable is greater than the supplied integer parameter.

Lession 54: Predicate delegate in C#

A predicate is also a delegate like  Func  and  Action  delegates. It represents a method that contains a set of criteria and checks whether the passed parameter meets those criteria or not. A predicate delegate methods must take one input parameter and it then returns a boolean value - true or false. The Predicate delegate is defined in the System namespace as shown below: Predicate signature:  public   delegate   bool   Predicate < in  T>(T obj); Same as other delegate types, Predicate can also be used with any method, anonymous method or lambda expression.

Lession 53: Action Delegate

Action is also a delegate type defined in the System namespace. An Action type delegate is the same as  Func delegate except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type. For example, the following delegate prints an int value.

Lession 52: Func delegate in C#

Image
We have learned in the previous section, that a  delegates  can be defined as shown below.

Lession 51: LINQ

LINQ (Language Integrated Query) is uniform query syntax in C# and VB.Net to save and retrieve data from different sources. It is integrated in C# or VB, thereby eliminating the impedance mismatch between programming languages and databases, as well as providing a single querying interface for different types of data sources. For example, SQL is a Structured Query Language to save and retrieve data from the database the same way LINQ is a structured query syntax built in C# and VB.Net to save and retrieve data from different types of data sources like an Object Collection, an SQL server database, XML, a web service etc. Learn LINQ step by step in LINQ tutorials section.

Lession 50: var - Implicit typed local variable in C#

C# 3.0 introduced the implicit typed local variable "var". Var can only be defined in a method as a local variable. The compiler will infer its type based on the value to the right of the "=" operator.

Lession 49: Covariance and Contravariance in C#

Image
Covariance and contravariance allow us to be flexible when dealing with class hierarchy. Consider the following class hierarchy before we learn about covariance and contravariance: Sample Class Hierarchy: class Small { } class Big : Small { } class Bigger : Big { } As per the above example classes, small is a base class for big and big is a base class for bigger. The point to remember here is that a derived class will always have something more than a base class, so the base class is relatively smaller than the derived class. Now, consider the following initialization: Class initialization As you can see above, a base class can hold a derived class but a derived class cannot hold a base class. In other word, an instance can accept big even if it demands small, but it cannot accept small if it demands big. Now, let's learn about covariance and contravariance. Covariance: Covariance enables you to pass a derived type where a base type is expe...

Lession 48: Nullable Type in C#

Image
As you know, a value type cannot be assigned a null value. For example,  int i = null  will give you a compile time error. C# 2.0 introduced nullable types that allow you to assign null to value type variables. You can declare nullable types using  Nullable<t>  where T is a type.

Lession 47: C# Anonymous method

As the name suggests, an anonymous method is a method without a name. Anonymous methods in C# can be defined using the delegate keyword and can be assigned to a variable of delegate type.

Lession 46: C# Static

Image
C# includes "static" keyword just like other programming languages such as C++, Java, etc. The  Static  keyword can be applied on classes, variables, methods, properties, operators, events and constructors. However, it cannot be used with indexers, destructors or types other than classes.

Lession 45: C# Dictionary

Dictionary in C# is same as English dictionary. English dictionary is a collection of words and their definitions, often listed alphabetically in one or more specific languages. In the same way, the Dictionary in C# is a collection of Keys and Values, where key is like word and value is like definition.

Lession 44: C# Generic SortedList

Image
A generic SortedList (SortedList<TKey,TValue>) represents a collection of key-value pairs that are sorted by key based on associated  IComparer<T>  A SortedList collection stores key and value pairs in ascending order of key by default. Generic SortedList implements IDictionary<TKey,TValue> & ICollection<KeyValuePair<TKey,TValue>> interfaces so elements can be access by key and index both.

Lession 43: C# List

You have already learned about ArrayList in the previous section. An ArrayList resizes automatically as it grows. The List<T> collection is the same as an ArrayList except that List<T> is a generic collection whereas ArrayList is a non-generic collection. List<T> can be initialized in the following two ways.

Lession 42: Generic Collection in C#

You have learned about the  collection  in the previous section, e.g.  ArrayList , BitArray,  SortedList ,  Queue ,  Stack  and  Hashtable . These types of collections can store any type of items. For example, ArrayList can store items of different data types:

Lession 41: Constraints in Generics

You have learned abut the generics in the previous section. Generics allow you to define a class with placeholders for the type of its fields, methods, parameters, etc. Consider the following example of a generic class.

Lession 40: Generics in C#

Image
Generics introduced in C# 2.0. Generics allow you to define a class with placeholders for the type of its fields, methods, parameters, etc. Generics replace these placeholders with some specific type at compile time. A generic class can be defined using angle brackets <>. For example, the following is a simple generic class with a generic member variable, generic method and property.

Lession 39: Event

Image
In general terms, an event is something special that is going to happen. For example, Microsoft launches events for developers, to make them aware about the features of new or existing products. Microsoft notifies the developers about the event by email or other advertisement options. So in this case, Microsoft is a publisher who launches (raises) an  event  and  notifies  the developers about it and developers are the  subscribers  of the event and attend ( handle ) the event.

Lession 38: C# Delegate

Image
A function can have one or more parameters of different data types, but what if you want to pass a function itself as a parameter? How does C# handle the callback functions or event handler? The answer is -  delegate . A delegate is like a pointer to a function. It is a reference type data type and it holds the reference of a method. All the delegates are implicitly derived from  System.Delegate  class. A delegate can be declared using  delegate  keyword followed by a function signature as shown below.

Lession 37: Create Custom Exception Class in C#

We have seen built-in exception classes in the previous section. However, you often like to raise an exception when the business rule of your application gets violated. So, for this you can create a custom exception class by deriving Exception or ApplicationException class.

Lession 36: C# - throw keyword

We have seen in the previous section how to handle exceptions which are automatically raised by CLR. Here, we will see how to raise an exception manually. An exception can be raised manually by using the throw keyword. Any type of exceptions which is derived from  Exception  class can be raised using the throw keyword.

Lession 35: Exception Handling in C#

We have seen in the previous section that an exception is thrown by the CLR or program code if there is an error in the program. These exceptions need to be handle to prevent crashing of program. C# provides built-in support to handle the exception using try, catch & finally block.

Lession 34: Exception in C#

Image
An application may encounter an error during the execution. When an error occurs, either CLR or program code throws an exception which contains necessary information about the error. There are two types of exceptions in .Net, exceptions generated by the executing program and exceptions generated by the CLR. C# includes built-in classes for every possible exception. All the exception classes are directly or indirectly derived from the  Exception  class. There are two main classes for exceptions -  SystemException  and  ApplicationException . SystemException is a base class for all CLR generated errors whereas ApplicationException serves as a base class for all application related exceptions, which you want to raise on business rule violation.