Pages

Wednesday, April 17, 2013

Extension Methods in c#

Extension Methods

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

namespace ExtensionMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 5;
            Console.WriteLine("Factorial of {0} number is{1}", x, x.fact());
            Console.ReadKey();
        }
    }
    static class factorial
    {
        public static int fact(this int number)
        {
            if (number <= 1) return 1;
            if (number == 2) return 2;
            else
            {
                return number * fact(number - 1);
            }
        }
    }
}

properties in c#

Properties

class student
    {
        public int studentID { get; set; }
        public string studentName { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            student obj = new student();
            obj.studentID = 116;
            obj.studentName = "satya";
            Console.WriteLine("Student ID {0} and Student Name {1}", obj.studentID, obj.studentName);
            Console.ReadKey();
        }
    }

Inheritance

 1.Simple Inheritance

 class baseClass
    {
        public baseClass()
        {
            Console.WriteLine("Base Class Constructor Called");
        }
        public void print()
        {
            Console.WriteLine("Base Class Print Method Called");
        }
    }
    class derivedClass:baseClass
    {
        public derivedClass()
        {
            Console.WriteLine("Derived Class Constructor Called");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            derivedClass obj = new derivedClass();
            obj.print();
            Console.ReadKey();
        }
    }
   
    2.Use of base and new keywords in Inheritance
   
     class Program
    {
        class baseClass
        {
            public baseClass()
            {
                Console.WriteLine("Base Class Constructor Called");
            }
            public baseClass(String newString)
            {
                Console.WriteLine(newString);
            }
            public void Print()
            {
                Console.WriteLine("Base Class Print Method Called");
            }
        }
        class derivedClass:baseClass
        {
            public derivedClass():base("From Child")
            {
                Console.WriteLine("Child Constructor Called");
            }
            public new void Print()
            {
                base.Print();
                Console.WriteLine("Child Class Print Method Called");
            }

        }
        static void Main(string[] args)
        {
            derivedClass obj = new derivedClass();
            obj.Print();
            ((baseClass)obj).Print();
            Console.ReadKey();
        }
    }
   
    keyword base call the base class constructor with the matching parameter list
    Using the base keyword, you can access any of a base class public or protected class members
    Another way to access base class members is through an explicit cast
    Notice the new modifier on the Child class print() method. This enables this method to hide the Parent class print() method and explicitly states your intention that you don't want polymorphism to occur.

Generic Collections

A generic collection is strongly typed (type safe), meaning that you can only put one type of object into it. This eliminates type mismatches at runtime. Another benefit of type safety is that performance is better with value type objects because they don't incur overhead of being converted to and from type object.
Creating Generic List<T> Collections
Ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lists
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.Add(4);
            foreach (var i in list)
            {
                Console.WriteLine(i);
                Console.WriteLine("\n");
            }
            Console.ReadKey();
        }
    }
}
The first thing you should notice is the generic collection List<int>, which is referred to as List of int. If you looked in the documentation for this class, you would find that it is defined as List<T>, where T could be any type. For example, if you wanted the list to work on string or Customer objects, you could define them as List<string> or List<Customer> and they would hold only string or Customer objects

Dictionary<TKey, TValue> Collections
Another very useful generic collection is the Dictionary, which works with key/value pairs. There is a non-generic collection, called a Hashtable that does the same thing, except that it operates on type object.

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

namespace Dictionary
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int, Customer> customers = new Dictionary<int, Customer>();
            Customer customer1 = new Customer(1, "customer1");
            Customer customer2 = new Customer(2, "customer2");
            Customer customer3 = new Customer(3, "customer3");
            customers.Add(customer1.customerID, customer1);
            customers.Add(customer2.customerID, customer2);
            customers.Add(customer3.customerID, customer3);

            foreach (KeyValuePair<int, Customer> cust in customers)
            {
                Console.WriteLine("CustomerID-{0} and CustomerName-{1}\n", cust.Key, cust.Value.customerName);
            }
            Console.ReadKey();
        }
    }
    class Customer
    {
        public Customer(int custID, string Name)
        {
            customerID = custID;
            customerName = Name;
        }
        public int customerID
        {
            get;
            set;
        }
        public string customerName
        {
            get;
            set;
        }
    }
}
Whenever coding with the generic collections, add a using System.Collections.Generic declaration to your file.There are many more generic collections to choose from also, such as Stack, Queue, and SortedDictionary.

Wednesday, April 3, 2013

Armstrong Number Program in C#

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

namespace ArmstrongNumber
{
    class Program
    {
        static void Main(string[] args)
        {int n,m,s=0,r;
            Console.WriteLine("Enter a Number");
            n=Convert.ToInt16(Console.ReadLine());
            m = n;
            while (n > 0)
            {
                r = n % 10;
                s = s + r * r * r;
                n = n / 10;
            }
            if (m == s)
                Console.WriteLine("Armstrong Number");
            else
                Console.WriteLine("Not an Armstrong Number");
            Console.ReadKey();
        }
    }
}

Palindrome Program in C#

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

namespace PrimeNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j = 2, flag = 1;
            Console.WriteLine("Enter a Number");
            i =Convert.ToInt16(Console.ReadLine());
            while (j <= i / 2)
            {
                if (i % j == 0)
                {
                    flag = 0;
                    break;
                }
                else
                {
                    j++;
                }
            }
            if (flag == 0)
                Console.WriteLine("Given Number is not a Prime");
            else
                Console.WriteLine("Given Number is Prime");
            Console.ReadKey();
        }
    }
}

Prime Number program in C#

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

namespace PrimeNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j = 2, flag = 1;
            Console.WriteLine("Enter a Number");
            i =Convert.ToInt16(Console.ReadLine());
            while (j <= i / 2)
            {
                if (i % j == 0)
                {
                    flag = 0;
                    break;
                }
                else
                {
                    j++;
                }
            }
            if (flag == 0)
                Console.WriteLine("Given Number is not a Prime");
            else
                Console.WriteLine("Given Number is Prime");
            Console.ReadKey();
        }
    }
}