Saturday, April 9, 2016

Collection in c# with example



Collection is the abstractions of data algorithms.Net offer different kinds of collection like Array list Hash table, Dictionaries, queues.

ArrayList Collection – An Arraylist is just like a dynamic array, each element of arraylist is accessed by the using indexing operator. In Arraylist we can add or delete item at run time.
The syntax of the ArrayList is given below:

Arraylist obj=new ArrayList();

We can add item by using the Add() method

Obj.Add(“Sachin”);
Obj.Add(“Sharad”);
We can remove the item by using Remove() method
Obj.Remove(“sachin”);

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
           
            ArrayList obj = new ArrayList();

       
            obj.Add("sachin");
            obj.Add("Sharad");
            obj.Add("Pramod");
          

         
            obj.Insert(2, "Amit");

           
            for (int i = 0; i < obj.Count; i++)
            {
                Console.WriteLine("At Index Value[" + i + "]= " + obj[i].ToString());
            }

       
            Console.ReadKey();
        }
    }
}


output :



Hashtable:
Hashtable works just like of ArrayList except that it is not required to use a numerical index.
The Syntax of the Hashtable is given below:
Hashtable obj = new Hashtable();

Benefits of Hashtable :

We can add many pairs of key/value element as required , we do not have to specify the size at a time
We can use text, number, dates as per the index.
It is very fast element.
We can remove any element very easily.

Limitations of Hashtable : 

It is required unique key
Sorting is not done by the key/value
The example of the HashTable is given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable obj = new Hashtable();

            obj[Convert.ToDateTime("01/31/1990")] = "My Birth Date";
            obj[Convert.ToDateTime("01/31/1990")] = "My First Birthday";
            obj[Convert.ToDateTime("01/31/2016")] = "My 26th Birthday";

        
            Console.WriteLine("Enter date 'Month/Date/Year' Format");
            DateTime DateData = DateTime.Parse(Console.ReadLine());

    
            Console.WriteLine(obj[DateData]);

         
            Console.ReadKey();
        }
    }
}

Output : 






Wednesday, April 6, 2016

Enum in c# with Example?



An Enum is user defined value data type. Enum is a group of related constant. The enum keyword is used to declare enumeration.Enums types can be float, int, byte, double etc.

The example of the enums is given below:

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

namespace WindowsFormsApplication12
{
    public enum operation
    {
        sum,sub
    }
    class Class1
    {
        public int calculate(int i, int j,operation o)
        {
            if (o == operation.sum)
            {
                return i + j;
            }
            else if (o == operation.sub)
            {
                return i - j;
            }
            return 0;
        }
    }
}

.cs code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
           int x= obj.calculate(10, 5, operation.sum);
           int y = obj.calculate(10, 5, operation.sub);
           MessageBox.Show(x.ToString());
           MessageBox.Show(y.ToString());
        }
    }
}
 
Output: 
15

 

Ref and out parameter in c#



Ref parameter: -The ref keyword will pass the parameter as a reference, if we want to pass a variable as a ref then we need to initialize it before pass it as reference parameter to method .The main features of the ref parameter is that when the value of the parameter is changed in called method it is also reflected in calling method.

Example:

class class1
{
    static void Main()
    {
        int i;
        i = 5;
        Ref(ref i);
        Console.WriteLine(i);
    }
    public static void Ref(ref int val)
    {
        val += 10;
    }
}

Output: 15

Out parameter - The out keyword will pass the parameter as a reference, if we want to pass a variable as a out then we need to initialize it before pass it as reference parameter to method .Out parameter is initialized in called method before it return the value to calling method.
class class1
{
    static void Main()
    {
        int i, j;
        Out(out i, out j);
        Console.WriteLine(i);
        Console.WriteLine(j);
    }
    public static int Out(out int val, out int val1)
    {
        val = 15;
        val1 = 10;
        return 0;
    }
}

Output : 15,10

Tuesday, April 5, 2016

Introduction to object oriented programming concept in c#



Object oriented programming language (Oops) is programming language model based on “objects” and data rather than action and logic. The features of the oops is given below:

Object – An object is real world entity that contain attribute and behavior is called object. For example pen, chair, car etc.

Class - Class is the collection of object.

Polymorphism – Ability to take more than one form is called polymorphism. For details about polymorphism read - http://dotnetdarpan.blogspot.in/2016/02/polymorphism-in-c-with-example.html

Inheritance – when the child class acquires all the properties of parent class i.e. known as inheritance. For more details read - http://dotnetdarpan.blogspot.in/2016/02/what-is-inheritance-in-c.html

Abstraction – act of representing essential features without including background details of explanation is called abstraction.

Encapsulation – binding or wrapping the data in single unit is called encapsulation.

Monday, April 4, 2016

Check Vowel using Switch Case in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class class1
    {
        static void Main(string[] args)
        {
            char c;
            Console.WriteLine("Enter the Charachter to chech it is vowel or not");
            c = char.Parse(Console.ReadLine());
            Console.WriteLine("please wait searching ........");
            System.Threading.Thread.Sleep(2000);
            switch (c)
            {
                case 'a':
                case 'A':
                case 'E':
                case 'e':
                case 'I':
                case 'i':
                case 'o':
                case 'O':
                case 'u':
                case 'U':
                    Console.WriteLine("{0} is a vowel", c);
                    break;
                default: Console.WriteLine("{0} is not a vowel", c);
                    break;
            }
            Console.ReadKey();
        }
    }

Sunday, April 3, 2016

What is event in c#?



Events are the members of the class that rises when something happens a class, which have a message that contain the information about the event, Event handler is a method that contain the same signature as the event and this method is execute when the event occurs.

When we define an event then first define a delegate that contains the methods that will be called when the event raised , the example of the event is given below : 

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

namespace WindowsFormsApplication11
{
    public delegate void mydel();
   public  class Class1
    {
        public event mydel myevent;
        public void fire()
    {
        myevent();
    }
    }
}

.cs code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            obj.myevent+=new mydel(obj_myevent);
            obj.fire();
        }
        void obj_myevent();
    {
        MessageBox.show("work done");
    }
    }

Output :
work done