Dictionary in C#


Dictionary is used to represent a collection of key and Value pair of data. It is a generic class and it can store any data type. It came with .net F/W 2.0.

Dictionary contains key and value together as shown in given example below


 protected void Page_Load(object sender, EventArgs e)
    {
        Dictionary<string, int> Emps = new Dictionary<string, int>();
        Emps.Add("Ram", 1);
        Emps.Add("Mohan", 2);
        Emps.Add("Hari", 3);
        foreach (var name in Emps)
        {
            lblmsg.Text += name.Key + " " + name.Value  +"<br>";
        }     
    }

Then output will come like this

Ram 1
Mohan 2
Hari 3

ContainKey and ContainValue Method

It has containkey and ContainValue method. Using ContainKey we can find the specific key in dictionary and using ContainValue we can get the specific value. It returns the Boolean value.


 if(Emps.ContainsKey("Hari"))
        {
            int value = Emps["Hari"];
            lblmsg.Text = value.ToString();
        }


It has also add,remove and Clear method.

Advantages

1. Dictionary type provides fast lookups with keys to get values.
2. It stores any datatype
3. It provide good performance as compare to HashTable

Getting Started with WPF ? Part1


What is the WPF?

WPF is the window presentation foundation. It is the next generation Presentation system for building window client application with visually good and stunning user experience.
WPF has come with .Net F/W 3.0. WPF contains Vector based rendering engine.

It contains the following features
1 .Has all equivalent common user controls like buttons, check boxes sliders etc.
2 .Fixed and flow format documents
3. Has all of the capabilities of HTML and Flash
4. 2D and 3D vector graphics
5. Animation
6. Multimedia
7. Data binding
8. Style
9.Template
Using WPF we can create both standalone and browser hosted application

Now let’s start some basic simple application in WPF
Step1: Start Visual Studio 2008/2010/2012,–> select new project –> WPF application

Then you will get the window like this image

WPF1

WPF application defaults contains the following files as rounded circle shown in the above image
1. Properties
2. References
3. App.xaml
4. MainWindow.xaml

Now lets know what is the use of above files in the project

Properties: It contains all the properties of project. If you want to see then just double click on it. It will look like this image
Properties_WPF

Using this files, we can change configuration of whole project. It is very important files in the project.

References: This file also available window/web/console/metro type project. It contains all the necessary dll to run the application.We have add .dll and service reference.

App.xaml : It is the application xaml file which contains the resources(style, pens, brushes, etc.) which can be shared in all forms. If you have worked in asp.net project then you can consider this like global.aspx. file. It is very handy files for resource sharing in whole application. It contains the information while form should loaded file . You can also changed the first start up file.

In WPF there is no similar concept like asp.net to make startup page. We can only do from this file
As shown below the image

app file

Mainwindow.xaml : It is the main form. Here we can design the form as per as our requirement. Any WPF form contains two files i.e Xaml and C# file

Xaml is Extensible application markup language. It is used for designing the form in WPF,Silverlight, Window Phone and Metro application.

Test the Hello program in WPF application
Step 1: Keep one button in Grid panel like this






<Button Name="button" Height="30" Width="200" Click="button_Click">Click Me!
            </Button>

Step 2: Click on click event of button and the write the code in code behind like this

private void button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(“Hello, Window Presentation Foundation”);
}

Step3: Press the F5 and click on button then you will get output like this

Hello

What is the abstract class ?


Hi
This is one of the frequently asked questions in interview. I have faced this question in almost in all interviews. So let’s understand what is abstract class?

Abstract class is the class which one cannot be instantiated. This means we canot create object of this class like general class. To use it, we need to inherit it. It contains the abstract keyword.

In abstract class, we can define
1. Normal Property
2. Abstract Property
3. Normal Method
4. Abstract Method
5. Virtual method.

Now let see this concept with example


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Class1
/// </summary>
public abstract class Class1
{
    public virtual string SayHello()
    {
        return "Hello from virual method";
    }

    public string sayHello2()
    {
        return "I m from SayHello Method";
    }

    public abstract string SayHello1();

}

Note: Here i have created the abstract class which contain Normal method,abstract method and Virtual method.

Now we will inherit this class in class2 like this


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Class2
/// </summary>
public class Class2:Class1
{
    public override string SayHello()
    {
        return "This is the orveride message using virtual method";
    }

    public override string SayHello1()
    {
        return "This is the orveride message using abstract message";
    }


}

Note: in the above code i have override the virtual and abstract method. So if virtual or abstract keyword is there then we have to must override it.

Now we will call this method in default.cs page like this on button click event


 protected void Button1_Click(object sender, EventArgs e)
    {
         Class2 obj = new Class2();
       
        Response.Write(obj.SayHello()+"<br>"); 
        Response.Write(obj.SayHello1()); 
        
    }

Now compile the code, we will get output like this

override

When to use abstract Class?

We can use this class in this senario

1. If we have created some class and we want to modify some methods with new feature and we have to use somewhere then we can use abstract class

2. If we are developing some product type project, where we have to keep on adding new features in coming days then we can use abstract class.

Note: This is all my understanding my abstract class, if you feel i missing some important point then let me know. You are always welcome.