Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, May 29, 2013

Find vowels character in a String

Introduction

Some one ask a question to me, how to find all vowels from a string without repeating vowels ( means you can find out vowels from a string but if one is come out like 'E' from that string, then next time it is not included with the output.

Now I write a code for Find out vowels from a given string.

Example

using System;
class Program
{
    static void Main()
    {
        string value1 = RemoveDuplicateChars("AERTJEIUO");
        for (int i = 0; i <= value1.Length - 1; i++)
        {
                // code that checks the char is vowel or not
            if (65 == (int)value1[i] || 69 == (int)value1[i] || 73 == (int)value1[i] || 79 == (int)value1[i] || 85 == (int)value1[i]) 
            {
                Console.WriteLine(value1[i]);
            }
        }
        Console.ReadKey();
    }
 
    // method for removing the duplicate characters
        static string RemoveDuplicateChars(string key)
    {
        string val1 = "";
        string result = "";
        foreach (char value in key)
        {
            if (val1.IndexOf(value) == -1)
            {
                val1 += value;
                result += value;
            }
        }
        return result;
    }
}

Output


Monday, May 6, 2013

Interfaces in C#


Introduction

An interface look like a class, but has no implementation, or say only one thing it contains are declaration of indexes, methods, events or properties, Interface in C# provide a way to achieve runtime ploymorphism ( means in which compiler identifies which polymorphism from to execute at run time but not at compile time is called as rum time polymorphism or late binding) . 
A simple example that shows how to look like an interface in c#

class Iexample
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello");
        }
    }
    interface demo
    {
    }
Output
 

Hello
 


No problem, the above program will be run successfully, the above program consist a

class Iexample,

Main() is the entry point and an interface with the name as demo, at this time Interface
demo is empty, Now

I try to add some element in it.

What can we not do with interfaces, Lets see
1- Interface can not contain variable or variables
class Iexample
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello");
        }
    }
    interface demo
    {
       int  x;
    }
When you compile this program it will  simply gives an error "Interface cannot contain

fields".
2- Interface member can not have definition.
  class Iexample
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello");
            Console.ReadKey();
        }
       }
 
    interface demo
    {
        void abc()
        {
            System.Console.WriteLine("Interface member can not have definition");
        }
 
    }
When you compile this program it will  simply gives an error "Interface can not have

definition
".
3- when you create an interface and inherit that interface by the any class, then you must implement that interface in that class, otherwise it will gives a error.
class Iexample:demo
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello");
            Console.ReadKey();
        }
       }
 
    interface demo
    {
        void abc();
       
    }
When you compile this program it will  simply gives an

error  "'consoleApplication1.Iexample' does not implement the interface member

consoleApplication1.demo.abc()
'".
4- when you create an interface and inherit that interface by the any class, then you must implement that interface in that class and that interface method must be implemented with access modifier "public" otherwise it will generate an error. Look a simple program.
class Iexample:demo
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello");
            Console.ReadKey();
        }
        void abc()
        {
            Console.WriteLine("Implement with public");
        }
       }
 
    interface demo
    {
        void abc();
       
    }
When you compile this program it will  simply gives an error "'consoleApplication1.Iexample' does not

implement the interface member 'consoleApplication1.demo.abc()
'" and also

"'consoleApplication1.Iexample' cannot not implement the interface member because it is not public."


Now we move out with right example of interface

Example of Interface (With method)
 
using System;
namespace ConsoleApplication11
{
    class Iexample:demo
    {
        static void Main(string[] args)
        {
            demo obj = new Iexample(); //decalare an interface instance
            obj.abc();
            System.Console.WriteLine("Bye....");
            Console.ReadKey();
        }
       public void abc()
        {
            Console.WriteLine("Fun with Interface");
        }
       }
 
    interface demo
    {
        void abc();
       
    }
   
}
 
Output
 

Fun with Interface
Bye....
 

As we say above, Interface contains are declaration of indexes, methods, events or properties, Now have

p
lay with method, Now Its time to play with properties.
Example of Interface (With properties)
using System;
namespace ConsoleApplication11
{
    class Iexample:InterfaceImnpelment
    {
        static void Main(string[] args)
        {
            InterfaceImnpelment obj = new Iexample();
            obj.Prop1 = 1;
            obj.Prop2 = "dothearts";
            Console.WriteLine("Rank of " + obj.Prop2 + " is " + obj.Prop1);
            Console.ReadKey();
        }
     
       }
    class InterfaceImnpelment : demo
    {
        private int _prop1;
        private string _prop2;
        public int Prop1
        {
            get { return this._prop1; }
            set { this._prop1 = value; }
        }
        public string Prop2
        {
            get { return this._prop2; }
            set { this._prop2 = value; }
        }
    }
    interface demo
    {
        int Prop1
        {
            get;
            set;
        }
        string Prop2
        {
            get;
            set;
        }
    }
   
}
Output
 

Rank of dotnethearts is 1

You can also implement more than one interface into a single, which have inherits its.
using System;
namespace ConsoleApplication11
{
    class Iexample : MultiInterfaces
    {
        static void Main(string[] args)
        {
            MultiInterfaces obj = new Iexample();
            obj.fun1();
            obj.fun2();
            Console.ReadKey();
        }
    }
    // implement more than one interface in a single class
    class MultiInterfaces : demo1,demo2 
    {
        public void fun1()
        {
            Console.WriteLine("Hi!");
        }
        public void fun2()
        {
            Console.WriteLine("Bye!");
        }
    }
    interface demo1
    {
        void fun1();
    }
    interface demo2
    {
        void fun2();
    }
}

Advantage of interface
  • It provide the facility to reuse of software.
  • By the help of interface you can hide the implementation details of classes from each other.
  • Code reusability.

Kashmir 370 and 35A : The Wound of india

क्या है जम्मू-कश्मीर में लागू धारा 370,35A पूर्ण विवरण Know about 370 act in Jammu एक बार फिर से राजनीति गलियारे में धारा 370,35A को ल...