C#

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday, 10 December 2014

Reverse a string c# :- Interview Question

Question :)
Input String : I am he
Output String : he am I

using System;
using System.Linq;

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

            string x = "I am he";

            var y = x.Split(' ');

            foreach (var z in y.Reverse())
            {
                Console.Write(z + " ");
            }
            Console.ReadLine();
        }
    }

}



Difference Between Virtual Method and Abstract Method(CAN vs MUST)


CASE 1: No Compilation Error[Virtual Method Can be implemented]

using System;

namespace ConsoleApplication1
{
    public abstract class A
    {
        public abstract void Method1();
        public virtual void Method2()
        {
            Console.WriteLine("Method2Base");
        }
    }

    public class B : A
    {

        //public override void Method2()
        //{
        //    Console.WriteLine("Method2Derived");
        //}

        public override void Method1()
        {
            Console.WriteLine("Method1");
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            A obj = new B();
            obj.Method1();
            obj.Method2();
            Console.ReadLine();
        }
    }

}

CASE 2:Compilation Error[Abstract Method Must be implemented]

using System;

namespace ConsoleApplication1
{
    public abstract class A
    {
        public abstract void Method1();
        public virtual void Method2()
        {
            Console.WriteLine("Method2Base");
        }
    }

    public class B : A
    {

        public override void Method2()
        {
            Console.WriteLine("Method2Derived");
        }

        //public override void Method1()
        //{
        //    Console.WriteLine("Method1");
        //}
    }


    class Program
    {
        static void Main(string[] args)
        {
            A obj = new B();
            obj.Method1();
            obj.Method2();
            Console.ReadLine();
        }
    }
}

Count Number of recurring character in a string

using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string x = "aaaaanurAg";
            var z = x.ToUpper().ToCharArray().Where(y=>y=='A').Count();
            Console.WriteLine(z);
            Console.ReadLine();
        }
    }

}

Output: