Sunday, 6 November 2016

Static Class

Static Class

There are two main features of static class.

1. we cannot create an object of static class.
2. It must and should contains only static members.

The main benefit of making static class is , we do not need to create an object of this class, all the members of this class are accessible with class name.

Declaration.

A static class can be created using "static" Keyword as shown below.



Note #  In static class we have to explicitly make the members as static, static class does not automatically makes it members as static.

Example











Hope this information  helpful to you. Thank you for taking time to look at my blog. ----- Syed Sadiq Ali.      








Array Vs ArrayList Vs List

Arrays.

 Arrays belongs to  System.Array

-Arrays are collection of same data type.(i.e. strongly typed)

 Arrays are fixed in length that cannot be changed during runtime.

 Generally, In arrays we will store the values with the index basis that will start with zero.  
 If you want to access values from arrays we need to pass index values.

 Look at the below example.


  
In the above example i have declared size as 2. Let's try to add one more item here .


It will gives the run time exception. Index out of Range Exception, Because we mentioned the size as 2 now we are adding 3rd element to same array. 

---------------------------------------------------------------------------------------------------------------


ArrayLists

ArrayLsits belongs to System.Collections.

ArrayLists are not strongly typed collection. It will store the values of Same data type or different data type.

ArrayLsits size will increase or decrease dynamically.




The limitation of these collections is that while retrieving items, you need to cast into the appropriate data type, otherwise the program will throw a runtime exception. It also affects on performance, because of boxing and unboxing.
To overcome this problem, C# includes generic collection classes in the System.Collections.Generic namespace.

------------------------------------------------------------------------------------------------------------



List<T>

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.


Points to Remember :

  1. List<T> stores elements of the specified type and it grows automatically.
  2. List<T> can store multiple null and duplicate elements.
  3. List<T> can be assigned to IList<T> or List<T> type of variable. It provides more helper method When assigned to List<T> variable
  4. List<T> can be access using indexer, for loop or foreach statement.
  5. LINQ can be use to query List<T> collection.
  6. List<T> is ideal for storing and retrieving large number of elements.


List<T> can be initialized in the following two ways.



Add Elements into List:


Use the Add() method to add an element into a List collection. The following example adds int value into a List<T> of int type.

Add() signature: void Add(T item)


You can also add elements at the time of initialization using object initializer syntax as below:




AddRange:


The AddRange() method adds all the elements from another collection.
AddRange() signature: void AddRange(IEnumerable<T> collection)

Note :The AddRange() method will only be applicable if you initialized with a List<T> variable. IList<T> doesn't include the AddRange() method.

Access List collection:

- Use a foreach or for loop to iterate a List<T> collection.

Example


- If you have initialized the List<T> with an IList<T> interface then use seperate foreach statement with implicitly typed variable:

Example

- Access individual items by using an indexer (i.e., passing an index in square brackets):

Example



- Use the Count property to get the total number of elements in the List.
Example



- Use for loop to access list as shown below:
Example :Accessing List using for Loop Example











Hope this information  helpful to you. Thank you for taking time to look at my blog. ----- Syed Sadiq Ali.      










Saturday, 5 November 2016

Boxing Vs UnBoxing

Boxing


 The operation of Converting a Value Type to a Reference Type is called Boxing and the reverse operation is called Unboxing.

 
The first line we created a Value Type Val and assigned a value to Val. The second line , we created an instance of Object Obj and assign the value of Val to Obj. From the above operation (Object Obj = i ) we saw converting a value of a Value Type into a value of a corresponding Reference Type . These types of operation is called Boxing.

UnBoxing


 

The first two line shows how to Box a Value Type . The next line (int i = (int) Obj) shows extracts the Value Type from the Object . That is converting a value of a Reference Type into a value of a Value Type. This operation is called UnBoxing.







Hope this information  helpful to you. Thank you for taking time to look at my blog. ----- Syed Sadiq Ali.   



   



Dictionary Vs HashTable

Dictionary

It returns error if we try to find a key which does not exist.

It is faster than Hashtable , because there is no Boxing and UnBoxing.

Dictionary is generic type, which means we can use it with any data ype.

Only public static members are thread safe.  (?..)


HashTable

It return NULL if we try to find out a key which does not exist.

It is slower than Dictionary , because it requires Boxing and UnBoxing.

Hashtable is not generic type.

All the members in Hashtable are thread Safe.









Hope this information  helpful to you. Thank you for taking time to look at my blog. ----- Syed Sadiq Ali.      







Dictionary

Dictionary

A Dictionary is collection of Key, Value Pairs.

Dictionary Class is Present in  System.Collections.Generic NameSpace.

when creating dictionary we need to specify the type for Key and Value.

Dictionary provides fast lookup for values using keys.

Keys in dictionary must be unique.


Initialize A Dictionary Object In C#:

As Dictionary work with two type of data (one for ‘key’ and one for ‘value’), it requires both these data types to be defined at the time of initialization. Also, the namespace “System.Collections.Generic” is required for using dictionary in your application.
The following snippet will do initialize a simple dictionary with string as both data type:


We can define some default initial values at the time of initialization also. The following examples will add two data pair in the object at the time of initializing:




Add/Remove Pairs In Dictionary Object:


Insert and remove operations are fairly easy with a dictionary object. For adding a new pair, you will need to pass both key and value as parameter in “Add” method on the object. Removing object are done only by the ‘key’ portion. You just need to pass it to object’s “Remove” method. Check the following c# code examples for adding/removing dictionary data pair to object:






Hope this information  helpful to you. Thank you for taking time to look at my blog. ----- Syed Sadiq Ali.      





Friday, 4 November 2016

Var Vs Dynamic

Var

1. It is Introduced in C# 3.0

2. It is Statically typed, which means type of variable is decided by the compiler at compile time.

3. We have to initialize at the time of declaration.

Look at the examples below for better understanding

example 1. Var a = "This is Sadiq";

 Looking at assignment of value compiler treats variable a is a type of string.

example 2.  Var a ; 

The above example will throw a compiler error, since the variable is not initialized.

example 3. Var a  = 1 ;   //Here it treats a is an integer type

        Var a  = "This is Syed";

It will throw an error, since the compiler has already decided the type of variable a as System.Int32  when the value 1 was assigned to it. Now assigning a string value violates the type safety.

----------------------------------------------------------------------------------------
Dynamic

1. It is Introduced in C# 4.0

2. It is Dynamically typed, which means type of variable is decided by the compiler at run time.

3. We don't need to initialize at the time of declaration.

Look at the example below for better understanding

example 1. dynamic a ;

          a = 1;   // it will work fine and compiles
  
          a = "This is Syed";   // it will work fine and compiles
        


It will compile and run , since the compiler creates the type for variable  a as System.Int32 and then recreates the type as string when the value  "This is Syed" is assigned to it.




Hope this information  helpful to you. Thank you for taking time to look at my blog. ----- Syed Sadiq Ali.      






Dispose Vs Finalize Methods

Dispose() Method

1. Dispose method is used to free unmanaged resources like files, data base      connections etc.

Now the question here is What is unmanaged resources? 

Here is the Answer ===>

The term "unmanaged resource" is usually used to describe something not directly under the control of the garbage collector. For example, if you open a connection to a database server this will use resources on the server (for maintaining the connection) and possibly other non-.net resources on the client machine, if the provider isn't written entirely in managed code.

2. To clear unmanaged resources we need to write a code manually to raise a Dispose() method.


3. Dispose() method belongs to IDisposable interface.

4. If we need to implement this method for any custom classes we need to inherit from IDisposable interface.



Finalize() Method

Finalize() method is also used to free unmanaged resources like files, data base connections etc.

It is automatically raised by Garbage Collection Mechanism whenever object goes out of scope.

Finalize() method belongs to Object class






Hope this information  helpful to you. Thank you for taking time to look at my blog. ----- Syed Sadiq Ali.      






String vs StringBuilder

String

String is Immutable.

Immutable means once we create an object we cannot modify. 

If we try to perform any operation like insert, replace or append on string it will simply discard the old value and it will create new instance in the memory to hold the new value.

String belongs to System Namespace.

Performance wise string is slow because it will create a new instance every time.



StringBuilder

StringBuilder is Mutable

Mutable means once we create StringBuilder object we can perform any operation like insert, replace or append without creating new instance every time.









Hope this information  helpful to you. Thank you for taking time to look at my blog. ----- Syed Sadiq Ali.      







.ToString() vs Convert.ToString()

.ToString() vs Convert.ToString()


"Convert.ToString(Variable)" handles NULL Values even the varaible value becomes NULL.

"Variable.ToString()" will not handle NULL Values and it throws a NULL reference exception error. 

So as a good practice using Convert function is always safe.









Hope this information  helpful to you. Thank you for taking time to look at my blog. ----- Syed Sadiq Ali.      







Thursday, 3 November 2016

C#

Introduction to OOPS Concepts

OOP stands for Object Oriented Programming. It is a programming methodology that uses Objects to build a system or web applications using programming languages like C#, Vb.net etc.
Here, Objects plays a very important role because it hides the implementation details and exposed only the needed functionalities and related stuff that is required to adopt it. We can access class properties and methods by creating class object that I’ll explain below in this tutorial.