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: 


Monday, 24 November 2014

Recurrence of numbers in an array

Write a program that finds in a given array of integers (in the range
[0…1000]) how many times each of them occurs.
Example: array = {3, 4, 4, 2, 3, 3, 4, 3, 2}Chapter 16. Linear Data Structures 677
2  2 times
3  4 times

4  3 times


using System;
using System.Linq;

namespace ConsoleApplication8
{

    class Program
    {
        
        static void Main()
        {

            int[] source = new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 6, 7 };

            foreach (var item in source.Distinct())
            {
                
                Console.WriteLine("{0} => {1} Times",item,source.Count(f => f == item));  
            }
            Console.ReadLine();
        }
    }
}



Sunday, 23 November 2014

Write a program that reads from the console a positive integer number N (N < 20) and prints a matrix of numbers as on the figures below:

Write a program that reads from the console a positive integer number
N (N < 20) and prints a matrix of numbers as on the figures below:


N = 3 
1 2 3
2 3 4
3 4 5

N = 4
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7


using System;

namespace ConsoleApplication8
{

    class Program
    {

        static void Main()
        {

            for (int i = 1; i <= 3; i++)
            {
                for (int j = 0; j <=2; j++)
                {
                    Console.Write(i+j);
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}






Friday, 21 November 2014

Create An Array with Non Default Value

namespace ConsoleApplication8
{

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

            int[] intArray = Enumerable.Repeat(100, 5).ToArray();

            foreach (var x in intArray)
            {
                Console.WriteLine(x);
            }
            Console.ReadLine();
        }
    }
}




Tuesday, 21 October 2014

Reading a XML

XML
<?xml version="1.0" encoding="utf-8" ?>
<country name="India">
  <state name="KARNATAKA"></state>
  <state name="UP"></state>

</country>

-------------------------------------------------------

using System;
using System.Xml;

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


            XmlReader reader = XmlReader.Create(@"c:\users\anayak\documents\visual studio 2012\Projects\XML\XML\XMLFile1.xml");

                while (reader.Read())
                {

                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "state"))
                    {
                        if (reader.HasAttributes)
                        {
                            Console.WriteLine(reader.GetAttribute("name"));

                        }

                    }
                }
            
            Console.ReadLine();

        }
    }
}

OUTPUT












Monday, 15 September 2014

Interview Question Asked on Pre Increment and Post Increment

using System;

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

            int i = 30;
            int j = ++i;
                
            Console.WriteLine(i);//31
            Console.WriteLine(j);//31

            Console.WriteLine("-----------------");

            int x = 30;
            int y = x++;

            Console.WriteLine(x);//31
            Console.WriteLine(y);//30

            Console.WriteLine("-----------------");

            int a = 30;
            a++;
            Console.WriteLine(a);//31
            Console.WriteLine("-----------------");

            int b = 30;
            ++b;
            Console.WriteLine(b);//31

            Console.WriteLine("-----------------");
            int c = 30;
            c= c- c++;
            Console.WriteLine(c);//0

            Console.WriteLine("-----------------");
            int d = 30;
            d = d - ++d;
            Console.WriteLine(d);//-1
            Console.ReadLine();


        }
    }
}

Wednesday, 30 July 2014

Convert One List to Another List without For loop or Foreach Loop

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

namespace OverloadingConstructor
{
    class TargetList
    {
        public TargetList(string Id,string FirstName,String LastName)
        {
            this.LastName = LastName;
            this.FirstName = FirstName;
            this.Id = Id;
        }
        public string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        
    }

    class SourceList
    {
        public string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    
    }

    class Program
    {
        public static TargetList PointFToPoint(SourceList pf)
        {
            return new TargetList(pf.Id,pf.FirstName,pf.LastName);
        }

        static void Main(string[] args)
        {
            List<SourceList> sourceList = new List<SourceList>();

            sourceList.Add(new SourceList { FirstName = "Anurag", LastName = "Nayak", Id = "1" });
            sourceList.Add(new SourceList { FirstName = "Abhishek", LastName = "Nayak", Id = "2" });
            sourceList.Add(new SourceList { FirstName = "Siba", LastName = "Dalai", Id = "3" });
            sourceList.Add(new SourceList { FirstName = "Manju", LastName = "Rath", Id = "4" });

            List<TargetList> Target = sourceList.ConvertAll(new Converter<SourceList, TargetList>(PointFToPoint));
            Console.ReadLine();
        }
    }

}

----Another Nice way to convert is 

List<TargetList> Target1 = sourceList.ConvertAll(x => new TargetList {  FirstName=x.FirstName, Id=x.Id}).ToList();


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

namespace OverloadingConstructor
{
    class TargetList
    {
       
        public string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public List<string> DepId { get; set; }
    }

    class SourceList
    {
        public string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public List<string> DepId { get; set; }
    
    }

    class Program
    {
    
        static void Main(string[] args)
        {
            List<SourceList> sourceList = new List<SourceList>();

            sourceList.Add(new SourceList { FirstName = "Anurag", LastName = "Nayak", Id = "1", DepId = new List<string> { "1","2","3"} });
            sourceList.Add(new SourceList { FirstName = "Abhishek", LastName = "Nayak", Id = "2", DepId = new List<string> { "11", "21", "31" } });
            sourceList.Add(new SourceList { FirstName = "Siba", LastName = "Dalai", Id = "3", DepId = new List<string> { "21", "22", "23" } });
            sourceList.Add(new SourceList { FirstName = "Manju", LastName = "Rath", Id = "4", DepId = new List<string> { "31", "32", "33" } });

            List<TargetList> Target1 = sourceList.ConvertAll(x => new TargetList {  FirstName=x.FirstName, Id=x.Id, DepId=x.DepId}).ToList();
            Console.ReadLine();
        }
    }

}







Stars upper half program

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

namespace OverloadingConstructor
{


    class Program
    {
        static void Main(string[] args)
        {
            int num = 8;
            int x;

            for (int i = 0, c = num; i <= num; i++, c--)
            {
                x = c;
                for (int j = 1; j <= i; j++)
                {
                    while (x >= 1)
                    {
                        Console.Write(" ");
                        x--;
                    }
                    Console.Write("* ");

                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
    }

}






Star Up and Down Program

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

namespace OverloadingConstructor
{


    class Program
    {
        static void Main(string[] args)
        {
            int num = 10,x;
            StringBuilder s = new StringBuilder();
            List<string> list = new List<string>();
            for (int i = 1, c = num; i <= num; i++,c--)
            {
                x = c;
                for (int j = 1; j <= i; j++)
                {
                    while (x >= 1)
                    {
                        Console.Write(" ");
                        s.Append(" ");
                        x--;
                    }
                    Console.Write("* ");
                    s.Append("* ");
                }
                if(i!=num)
                list.Add(s.ToString());
                s.Length = 0;
                Console.WriteLine();
            }

            foreach (string stringfinal in list.OrderByDescending(x1=>x1.Length))
            {
                Console.WriteLine(stringfinal);
            }
            Console.ReadLine();
        }
    }
}


Trick 2

using System;
using System.Collections.Generic;

namespace StarUpDown
{
    class Program
    {
        static void Main(string[] args)
        {
           
            List<string> listStr = new List<string>();
            int x = 10;
            for (int i = 1; i <= x; i++)
            {
                string s = "";
                int count = x - i;
                for (int j = 1; j <=i; j++)
                {
                    while (count > 0)
                    {
                        Console.Write(" ");
                        count--;
                        s +=" ";
                    }
                    Console.Write("* ");
                    s += "* ";
                 

                }
                if (i != x)
                listStr.Add(s);
                Console.WriteLine();
            }

            listStr.Reverse();

            foreach (var item in listStr)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}





A program on Star

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

namespace OverloadingConstructor
{


    class Program
    {
        static void Main(string[] args)
        {
            string[,] array = new string[5, 11];

            for (int i = 0; i <= 4; i++)
            {
                for (int j = 0; j <= 10; j++)
                {
                    array[i, j] = "*";
                }
                Console.WriteLine();
            }

            for (int k = 0; k <= 4; k++)
            {
                for (int l = (k+1); l <=(10-(k+1)); l++)
                {
                    array[k, l] = " ";
                }
            }

            for (int i = 0; i <= 4; i++)
            {
                for (int j = 0; j <= 10; j++)
                {
                   Console.Write(array[i, j]);
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
    }
}





Wednesday, 23 July 2014

Basics of String Literals and Verbatim String Literal


èString Literal is a set of characters enclosed by double quotes.
“this is string literal”

    class Program
    {
        static void Main(string[] args)
        {
            string s = @"this is string1
literal";
            Console.WriteLine(s);
            Console.ReadLine();
        }

    }





è class Program
    {
        static void Main(string[] args)
        {
            string s = "this is string1 \nliteral";
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }

èVerbatim string literal starts with @ , which is followed by a quoted string. The contents of the quoted string are accepted without modification and can span two or more lines. So we can avoid escape sequence.

class Program
    {
        static void Main(string[] args)
        {
            string s = @"this is ""string1"" literal";
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }





class Program
    {
        static void Main(string[] args)
        {
            string s = "this is \"string1\" literal";
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }

ð Verbatim string literals are wonderful benefit for many formatting situation.

Sunday, 20 July 2014

Constructor Initialization

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

namespace OverloadingConstructor
{
    class A
    {
        public int a, b;

        public A():this(0,0)
        {
            Console.WriteLine("Inside A()");
        }

        public A(int a, int b)
        {
            this.a = 10;
            this.b = b;
            Console.WriteLine("Inside A(int a, int b)");
        }

        public A(A obj):this(obj.a,obj.b)
        {
            Console.WriteLine("Inside A(obj)");
        }
   
    }


    class Program
    {
        static void Main(string[] args)
        {
            A obj = new A(8,9);
            A objduplicate = new A(obj);
            Console.WriteLine("obj.a-->" + objduplicate.a + " " + "obj.b-->" + objduplicate.b);



            Console.ReadLine();


        }
    }

}