Tuesday, April 1, 2014

Dictionaries and Hash Table in C#

In this article am going to explain you concept of dictionaries in C#, from base to the level afterwards I’ll be discussing about the concept of types of dictionaries and concept of hash table and it’s related useful operation with their respective example.

Dictionaries

·        -   Dictionaries are used to associate a particular key with a given value.
·        -   Dictionaries are collection classes.
·        -   Functionality or access to the dictionary in C# is provided using-
   
using System.Collections;

·         -  A dictionary uses KEYS, for their functioning.
·         -  Dictionaries don’t have any sense of order.
·         -  Keys, works as lookups for table.

·         -  A dictionary contains various set of members in its declaration.



Now am going to discuss about hash table.

Hash Table | Dictionaries

·         -  Hash Table is an example of dictionary.
·         -  Declared in the same fashion as you declare other classes in C#.
·         -  Declared using this function
            Hashtable HT = new Hashtable();

·         This will create a new hash table, in which you can add data and perform other operations.

Operations | Hash Table

The major operations of hash table are-
 - Add
 - Remove

 - Copy
 - Count
 - Test
Add Operation | Hash Table

Adding Data to the Hash Table

There are two methods of adding data in your hash table in C#, you can both use or any of them in your code. I’ll show both methods through example.

Method: 1

In method 1 for adding data we do use-
            HT.Add("CShub", "cshub.somee.com");
            HT.Add("GoogleSays", "jaiswalabhishek.blogspot.in");
            HT.Add("Picmaniac", "picmaniac.brinkster.net"); 

Method: 2

In method2 we do use-
            HT["GoogleSays"] = "jaiswalabhishek.blogspot.in";
            HT["CShub"] = "cshub.somee.com";

Example | Hash Table

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Hello_Word
{
    class Program
    {
        static void Main(string[] args)
        {

            Hashtable HT = new Hashtable();
            HT.Add("CShub", "cshub.somee.com");
            HT.Add("GoogleSays", "jaiswalabhishek.blogspot.in");
            HT.Add("Picmaniac", "picmaniac.brinkster.net"); 


            Console.WriteLine("Address for website {0} is {1}  ", "GoogleSays", HT["GoogleSays"]);

            Console.ReadLine();
        }
     }
}



This is the second method for same functionality-

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Hello_Word
{
    class Program
    {
        static void Main(string[] args)
        {

            Hashtable HT = new Hashtable();
           // HT.Add("CShub", "cshub.somee.com");
           // HT.Add("GoogleSays", "jaiswalabhishek.blogspot.in");
           // HT.Add("Picmaniac", "picmaniac.brinkster.net"); 
            HT["GoogleSays"] = "jaiswalabhishek.blogspot.in";
            HT["CShub"] = "cshub.somee.com";

            Console.WriteLine("Address for website {0} is {1}  ", "GoogleSays", HT["GoogleSays"]);

            Console.ReadLine();
        }
     }
}



Count Operation | Hash Table

§  -   As like other languages in C# count is used for counting total number of data values available in the hash table.
§ -   There is no sense of order in the hash table for this operation.
§ -   Count operation is performed by-
Console.WriteLine("Total websites of abhishek jaiswal are {0}", HT.Count);

Example | Count

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Hello_Word
{
    class Program
    {
        static void Main(string[] args)
        {

            Hashtable HT = new Hashtable();
            HT.Add("CShub", "cshub.somee.com");
            HT.Add("GoogleSays", "jaiswalabhishek.blogspot.in");
            HT.Add("Picmaniac", "picmaniac.brinkster.net"); 


           // Console.WriteLine("Address for website {0} is {1}  ", "GoogleSays", HT["GoogleSays"]);
            Console.WriteLine("Total websites of abhishek jaiswal are {0}", HT.Count);

            Console.ReadLine();
        }
     }
}



Remove Operation | Hash Table

Remove operation is used for removing data entries from your hash table. This is simply performed by using-
            HT.Remove("GoogleSays");

Example | Remove

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Hello_Word
{
    class Program
    {
        static void Main(string[] args)
        {

            Hashtable HT = new Hashtable();
            HT.Add("CShub", "cshub.somee.com");
            HT.Add("GoogleSays", "jaiswalabhishek.blogspot.in");
            HT.Add("Picmaniac", "picmaniac.brinkster.net");

            HT.Remove("GoogleSays");
            if (HT.ContainsKey("GoogleSays"))
            {
                Console.WriteLine("Address for website {0} is {1}  ", "GoogleSays", HT["GoogleSays"]);
                // Console.WriteLine("Total websites of abhishek jaiswal are {0}", HT.Count);
            }

            Console.ReadLine();
        }
     }
}






Monday, March 31, 2014

Access Modifiers

In this article am going to discuss about access modifiers and their functionality along with, how they differ from each other and what are the basics of all these.
I’ll include all 4 access modifiers in this article.

Access Modifiers in C#

·         By default, data and methods defined in a class can only be used inside that class itself.
·         We must choose to expose them.
·         Access modifiers let you specify how programs that use your classes can access their data and methods.

 Class Access Modifiers | C#

Am going to explain this through an example-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Hello_Word
{
    class Program

    {
       static void Main(string[] args)

                public int Value;
      // public access modifier

                private string Message;
      // private access modifier

                protected void function();
      // protected access modifier


        }
}



We can use any of the class modifiers in our class but working mechanism of all these modifiers are different.


Modifiers | Types

Public

PUBLIC modifier is used at that place in the class of which values, attributes can be easily accessed by the any other object.

Private

PRIVATE access modifier is used where we want the functionality of attribute of that class can be only used within that class only.

Protected

-          It is used where the class member can only be accessed within the same class.
-          Inherited class of that class(child/subclass)

 Internal

-         -    It is the advanced functionality of C# 5.0 or above.
-         -    In this modifier class member can be accessed by any class in the same assembly.
-         -    Package of code like a library or other program.

Example

There are 2 parts in this code; first part is representing construction building and access modifiers definitions. In the second code there is a calling of the access modifiers functionalities.

Part: 1 | Code

// Book.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hello_Word
{
    class Book

    {
        // access modifier- PUBLIC
        public string Name;
        public decimal Price;
        public string Description;

        // access modifier- PRIVATE
        private decimal discount;

        // public constructer
        public Book(string bookName, decimal price)      

        {
            Name = bookName;
            Price = price;
            discount = 0.0m;
        }
    }
}

Part: 2 | Code

    class Program
    {
        static void Main(string[] args)
        {

            Book b1 = new Book("Black Book", 1050.00m);
            Book b2 = new Book("Complete Refrence", 1250.00m);

            string str = b1.Name;

          }
     }


When you do try to define b1.discount there will be no option in the list as




But if you do try to declare, it will show an error like this


// Program.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Hello_Word
{
    class Program
    {
        static void Main(string[] args)
        {

            Book b1 = new Book("Black Book", 1050.00m);
            Book b2 = new Book("Complete Refrence", 1250.00m);

            string str = b1.Name;

            decimal dsc = b2.discount;
        }
     }
}


// We can not directly use discount in our next file, because it is defined under a private class, we didn’t get the options from the list but if we do try to do so, it will surely show an error-
“Inaccessible due to private modifier”



Same in the case of protected modifier, if you try to access its properties it will again show the same message-



___________________________________________________________

For more technical blogging stuffs please visit,
http://www.c-sharpcorner.com/authors/2072a9/abhishek-jaiswal.aspx

___________________________________________________________