Xceedas

Xceedas
xceedas

Tuesday, 14 October 2014

Importance of Generics in C#

Introduction
Generics (in  the "System.Collections.Generic" namespace) were introduced in the 2.0 version of .Net and are now widely used. The concept is similar to C++ templates but different in implementation and capabilities. It is a very important feature of .Net programming, allowing a class and methods such as lists, hash tables, queues and so on to be defined with a type T as a parameter.

The type of generic parameter is specified only at the time of declaration. Using this, the programmer will improve the performance of an application. In addition to performance, generics provide type-safety and higher quality code because you get to reuse data processing algorithms without duplicating type-specific code.
Generics are also known as Parametric polymorphism.

Example
You can write a single class of generic type and we can use that generic class just by applying the appropriate datatype. For example I create a class MyClass<T> and we can use it like MyClass<int>, MyClaas<String>, MyClass<double> or MyClass< AnyClass> without the need for casting and unboxing.  For examle, consider a scenario where we need a different kind of queue, like a queue of int, string so we can create a generic type queue and based on our needs we can use that.

Advantages of using generic
ArrayList dynamically resizes. As elements are added, it grows in capacity to accommodate them. It is most often used in older C# programs. It stores a collection of elements of type object. Casting is needed. ArrayList is present in the System.Collections namespace.

Generics provide a solution for the limitation of an ArrayList collection in the Common Language Runtime. We will see the limitations of non-generic collection classes, using the ArrayList collection class.
  1. using System;  
  2. using System.Collections;  
  3.   
  4. public class ArrayListExample  
  5. {  
  6.     static void ArrayListMethod()  
  7.     {   
  8.         // Creates and initializes a new ArrayList.  
  9.         ArrayList listExample = new ArrayList();  
  10.         listExample.Add("One");  
  11.         listExample.Add("2");  
  12.         listExample.Add(3);          
  13.     }  
  14. }  
In the ArrayList Collection, any reference or value type data added to an ArrayList must be typecast to "System.Object". If the items are value types then they must be boxed when they are added to the list and unboxed when they are retrieved. The casting, boxing, unboxing operation reduce the performance. 

In a generic List<T> collection, using the "System.Collections.Generic" namespace, we will see the same operation. 
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. public class GenericList  
  5. {  
  6.     public static void GenericListMethod()  
  7.     {  
  8.        List<int> genericList = new List<int>();  
  9.        // No boxing, no casting:  
  10.        genericList.Add(12);  
  11.        genericList.Add(13);  
  12.        genericList.Add(14);  
  13.        genericList.Add(15);   
  14.     }  
  15. }  
 As compared to ArrayList it would be safer and faster, because in ArrayList we can enter anything since it accepts the value of object type. Here we can only enter type specific data like int, string or double.

Code Reusability in generics  (Advantage)

Generics provide type-safe code with re-usability.

Example
Suppose you need to sort the integer and floating type numbers, let's see how to do it in collections and generics.

The following shows how to do it in a collection:
  1. using System.Collections;  
  2.    
  3. public class GenericArrayList  
  4. {  
  5.     public static void ListSorting()  
  6.     {  
  7.         int[] intArray = { 8, 10, 2, 6, 3 };   
  8.   
  9.        // Sort your int array  
  10.         Array.Sort(intArray);  
  11.   
  12.         foreach (var i in intArray)  
  13.         {  
  14.             Console.WriteLine(i);  
  15.         }  
  16.   
  17.         // Sort it the other way   
  18.         string[] strArray = { "A", "D", "C", "E", "B" };  
  19.         Array.Sort(strArray);   
  20.   
  21.         foreach (var i in strArray)  
  22.         {  
  23.             Console.WriteLine(i);  
  24.         }  
  25.   
  26.         Console.ReadLine();  
  27.     }  
  28. }  
The following shows how to do it using Generics:
  1. using System.Collections.Generic;   
  2.   
  3. public class ArrayList  
  4. {  
  5.     private static object[] Sort<T1>(object[] iInputeArray)  
  6.     {  
  7.         array.Sort(iInputeArray);  
  8.         return iInputeArray;  
  9.     }  
  10. }  
Here T is a type. Once we create this method, we can call it with various data types as follows.
 
In this way a Generic provides code re-usability.
  1. public  static void SortList()  
  2. {  
  3.     object[] iInputArray = { 3, 5, 8, 6, 10, 6, 2, 1, 12 };  
  4.     iInputArray = Sort<int>(iInputArray);   
  5.   
  6.     foreach (Object obj in iInputArray)  
  7.     Console.Write("   {0}", obj);   
  8.   
  9.     object[]strInputArray = { "A" ,"C", "T", "R", "E", "D"  };  
  10.     strInputArray = Sort<string>(strInputArray);  
  11.   
  12.     foreach (Object obj in strInputArray)  
  13.     Console.Write("   {0}", obj);  
  14.   
  15.     Console.WriteLine();  
  16.     Console.ReadLine();  
  17. }  
Boxing: Conversion of a value type into a reference type of variable. When the variable of a value type is to be converted to a reference type, the object box is allocated to hold the value and copies the value into a box.

Unboxing: It is just the opposite of boxing.   
                                                               
Example Boxing/Unboxing
  1. class BoxUnbox  
  2. {  
  3.   Static void Main ()  
  4.   {  
  5.      Int i=1;  
  6.      Object o=i;    //boxing  
  7.      Int j = (int) o; //unboxing  
  8.   }  
  9. }   
Generic Class

The common use of a generic class is with collections like a linked list, hash table, stack, queue and tree. The addition and removal of an item from a collection is basically the same.

Example  
  1. using System.Collections.Generic;  
  2. namespace Generic  
  3. {
  4.     public class ArrayClass<T>  
  5.     { 
  6.         object Data;  
  7.         public void Push(object Obj)  
  8.         {  
  9.             Data= Obj;  
  10.         }  
  11.         public object Pop()  
  12.         {  
  13.             return Data;  
  14.         }  
  15.     }  
  16. }  
Here T is a type of parameter.  We can pass any data type (int, string and so on) as a parameter.

This class can be used as below.
  1. public class SubClass  
  2. {  
  3.     public static void MyClass()  
  4.     {  
  5.         ArrayClass<int> intArray = new ArrayClass<int>();  
  6.         intArray.Push(10);  
  7.         object intReturnValue = intArray.Pop();  
  8.         Console.Write("   {0}", intReturnValue);   
  9.         ArrayClass<string> strArray = new ArrayClass<string>();  
  10.         strArray.Push("Hello word");  
  11.         object strReturnValue = strArray.Pop();  
  12.         Console.Write("   {0}", strReturnValue);  
  13.         Console.ReadLine();  
  14.     }  
  15. }  
Generic Methods

Generic methods are a method declared with type parameters.

Examples
  1. using System.Collections.Generic;  
  2.   
  3. public class ArrayList  
  4. {  
  5.     static void Swap<T>(ref T lhs, ref T rhs)  
  6.     {  
  7.         T temp;  
  8.         temp = lhs;  
  9.         lhs = rhs;  
  10.         rhs = temp;  
  11.     }  
  12. }  
The following method shows how to call the method above.
  1. public static void TestSwap()  
  2. {  
  3.     int a = 1;  
  4.     int b = 2;  
  5.     Swap<int>(ref a, ref b);  
  6.     System.Console.WriteLine(a + " " + b);  
  7.     Console.ReadLine();  
  8. }  

Samsung Introduces New Wi-Fi With Speed 4.6Gbps

Today, Samsung announced the development of its new 60GHz Wi-Fi technology that increases wi-fi speed to 5x of the current maximum speed.
In the announcement, Samsung confirms data transmission speeds of up to 4.6Gbps, or 575MB per second, a five-fold increase from 866Mbps, or 108MB per second, the maximum speed possible with existing consumer electronics devices. Using this new Wi-Fi, a 3GB movie download will take less than 10 seconds.
“Samsung has successfully overcome the barriers to the commercialization of 60GHz millimeter-wave band Wi-Fi technology, and looks forward to commercializing this breakthrough technology,” said Kim Chang Yong, Head of DMC R&D Center of Samsung Electronics. “New and innovative changes await Samsung’s next-generation devices, while new possibilities have been opened up for the future development of Wi-Fi technology.”

Unlike the existing 2.4GHz and 5GHz Wi-Fi technologies, Samsung’s 802.11ad standard 60GHz Wi-Fi technology maintains maximum speed by eliminating co-channel interference, regardless of the number of devices using the same network. By doing so, Samsung’s new technology removes the gap between theoretical and actual speeds, and exhibits actual speed that is more than 10 times faster than that of 2.4GHz and 5GHz Wi-Fi technologies.

Wednesday, 8 October 2014

Simplest Way to Learn Object Oriented Programming

SIMPLEST WAY TO LEARN OOP

OOP1.jpg

What is a Class?

A class is a group of similar methods and variables. A class contains definitions of variables, methods etcetera in most cases.

or

A class is simply a representation of a type of object. It is the blueprint that describes the details of an object. A class contains member variables, methods and properties.

OOP2.jpg
What is an Object?
An instance of a class is known as an object. Using that object we can access the properties and methods of a class. The set of activities that the object performs defines the object's behavior. For example a Student (object) can provide the name or address. 

In pure OOP terms an object is an instance of a class. 

OOP3.jpg

What is Encapsulation?

"Encapsulation is a process of binding data members (variables and properties) and member functions (methods) into a single unit". And a class is the best example of encapsulation. 

OOP4.jpg

What is Inheritance?
Inheritance enables you to create a new class that inherits the properties from another class or base class and the class that inherits those members is called the derived class. So the derived class has the properties of the base class and its own class properties as well.

OOP5.jpg
What is Polymorphism?
In simple words we can say that whenever we are overloading the methods of a class it is called polymorphism. Or you can say polymorphism is often expressed as "one interface, multiple functions".

OOP6.jpg

What is an abstraction?

When we drive a car we often need to change the gears of the vehicle but we are not concerned about the inner details of the vehicle engine. What matters to us is that we must shift a gear, that's it. This is abstraction; show only the details that matter to the user.

Abstraction is used to manage complexity. Software developers use abstraction to decompose complex systems into smaller components.

OOP7.jpg
.
 

Thursday, 2 October 2014

E-commerce Application training available

E-mail me for details akinfaks@yahoo.com
+2348086643801