Sunday 27 April 2014

Access Specifiers in C#


Access Specifiers defines the scope of a class member (variable or function).

Five types of access specifiers:

  • Public Access Specifiers - can be accessed from anywhere even outside the namespace
  • Private Access Specifiers - hides its member variable and method from other class and methods. However, get(return value- retrieving value from private field)  set(return void - store value in private variables) property can retrieve or store private access modifiers values.
  • Protected Access Specifiers - member variable and method can only be accessed in child class
  • Internal Access Specifiers - hides its member variables and methods from other classes and objects in other namespace
  • Protected Internal Access Specifiers.- allows its members to be accessed in derived class, containing class or classes within the same namespace.

Dictionary in C#


Category
Features
Name Space
Inherits from
IDictionary
1.      IDictionary implementations fall into three categories: read-only, fixed-size, variable-size.
ICollection
IDictionary<TKey, TValue>
1.      The IDictionary<TKey, TValue> interface is the base interface for generic collections of key/value pairs.
2.      IDictionary is a "more generic" type .
ICollection<KeyValuePair<TKey, TValue>
Dictionary
This implementation does not provide a synchronized (thread-safe) wrapper for a DictionaryBase, but derived classes can create their own synchronized versions. Solution: Lock collection to avoid changes made by other threads
 
IDictionary
Dictionary<TKey, TValue>
1.      Provides a mapping from a set of keys to a set of values.
2.      Retrieving a value by using its key is very fast because class is implemented as a hash table.
3.      A Dictionary<TKey, TValue> can support multiple readers concurrently, as long as the collection is not modified.
 
IDictionary<TKey, TValue>

IEnumerable, ICollection, IList and List an Overview



Category
Methods
Name Space
Inherits from
Use
IEnumerable
GetEnumerator
System.Collections
 
Provided read-only access to elements in that collection via iteration.
IEnumerable<T> (Type safe version)
GetEnumerator
System.Collections.Generic
IEnumerable
ICollection
1.      Defines size, enumerators
2.      Asynchronization methods for all nongeneric collections.
System.Collections.ICollection
Modify the collection and care about its size.
ICollection<T>
Defines methods to manipulate generic collections.
System.Collections.Generic.ICollection
IEnumerable<T>
IList
1.      Adding/  removing elements
2.      Clear the collection.
3.      Handle  positioning of the elements
4.      Provides an object indexer to allow the user to access the collection
 eg myList[elementIndex]
 
ICollection
Modify the collection and ordering and / or positioning of the elements in the collection.
 
IList<T>
Some new methods for accessing a collection with specific positioning are present.
ICollection<T>
List
Dynamically resize arrays
 
IList
Ideal for linear collections not accessed by keys
List<T>
1.      Add  items
2.      Use an equality comparer
3.      accepts null as a valid value for reference types  
4.      Allows duplicate elements.
5.      Type safe than ArrayList
Never have own member implementations with the concrete type List/List<T>.

C# IEnumerable

  • IEnumerable is an interface.
  • It is returned from query expressions.
  • It implements the GetEnumerator method.
  • It enables the facility to use foreach-loop.
  • It permits the use of extension methods in the System.Linq namespace.
  • ToList and ToArray conversion is possible to an IEnumerable instance.
     
In the following example the Display method accepts a list of names as arguments and displays them in the Console
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
 
 
class Program
{
    static void Main()
    {
            Display(new List<string> { "Anu", "James", "Sam" });
    }
 
    static void Display(IEnumerable<string> names)
    {
            foreach (string value in names)
                Console.WriteLine(value);
    }
}
 
 
The same example is modified to accept a list of array values as arguments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IEnuExample1;
 
 
 
class Program
{
    static void Main()
    {
        string[] names = new string[] { "Anu", "James", "Sam" };
 
        Display(names);
 
 
    }
 
    static void Display(IEnumerable<string> namesArg)
    {
            foreach (string value in namesArg)
                Console.WriteLine(value);
    }
}
 
The following example  convert object data into a List instance:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace EnumEx2
{
  
 
class Program
{
    static void Main()
    {
                    //
                    // Use this input string[] array.
                    // ... Convert it to a List with the ToList extension.
                    //
                    string[] array = new string[]
                    {
                        "Anu", "James", "Sam"
                    };
                    List<string> list = array.ToList();
                    //
                    // Display the list.
                    //
          
 
                    foreach (string value in list)
                    {
                        Console.WriteLine(value);
                    }
    }
}
 
}
 
The following example sorts names using the LINQ syntax and displays these sorted elements in an array variable:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace EnumExample3
{
 
 
    class Program
    {
        static void Main()
        {
            //
            // Use this input string[] array.
            // ... Convert it to a List with the ToList extension.
            //
            string[] array = new string[]
                    {
                        "James", "Sam", "Anu"
                    };
 
            // Use query expression on array.
            //
            var query = from element in array
                        orderby element
                        select element;
 
            Console.WriteLine(array.Count());
 
            string[] array2 = query.ToArray();
            //
            // Display array.
            //
            foreach (string value in array2)
            {
                Console.WriteLine(value);
            }
 
        }
    }
 
}