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();
        }
    }
}

Factorial Program in C#

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

namespace Factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, n, fact = 1;
            Console.WriteLine("Enter a Number");
            n = Convert.ToInt16(Console.ReadLine());
            for (i = 1; i <= n; i++)
            {
                fact = fact * i;
            }
            Console.WriteLine("Factorial of {0} is {1}",n,fact);
            Console.ReadKey();
        }
    }

}

Maximum length of Query String?

Maximum length of Query String is based on browser not depend on the ASP.NET

Max.Length of a Query String in IE4.0 and above is ~2048 characters
opera-2048 characters
Netscape6 supporsts -2000 characters
Firefox supports-6000 characters

What are the parameters in C#?

There are two types of parameters are there in c#

1.Ref parameter

paramerer should be intilized before passing as ref parameter

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

namespace Parameteres
{
    class Program
    {
        static void Main(string[] args)
        {
            int i=0;
            Program obj = new Program();
            obj.show(ref i);
            Console.WriteLine(i);
            Console.ReadKey();

        }

        public void show(ref int i)
        {
            i = 10;
        }
    }
}


2.out paramete we no need to initilize the values, and both the methods we can retrun multiple values

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

namespace Parameteres
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j ;
            Program obj = new Program();
            obj.show(out i, out j);
            Console.WriteLine(i);
            Console.WriteLine(j);
            Console.ReadKey();

        }

        public void show(out int i,out  int j)
        {
            i = 10;
            j = 20;

        }
    }
}

Authetication in ASP.NET

There are three types of Authetication modes are there in ASP.NET

1.Form Authentication
2.Windows Authentication
3.Passport Authentication

How can we create Assembly in GAC?

There are 3 methods
1stmethod:By using command GAC UTIL -I
By using this command in visual studio command prompt

2ndmethod:Whatever we are created, we can drag to the GAC

3rdmethod:
Strong Name Concept:

Strong Name is a public key token of the assembly, which is different(Unique) for each Assembly

By using SNK properties one option is there sign of assembly(When we deploy automatically strong will create)

without strong name we cannot create assembly in GAC.

Benifit of StrongName :

we are creating an Identifier for Assembly that is Unique for each Assembly.

For ex: two assemblies are there Assembly A, Assembly A having same name

While calling conflict will come , application will get confuse which Assembly can be called , When the  Assemblies having same name and same version that time application will get confuse, which one has to use ,this is called dll hell problem.

Name Space: We can give logical group name to types(Classes, Interfaces)

System.dll Assembly
(NameSpace)

It consists of objects,classes,public access specifiers, string class...etc

NameSpace A ,NameSpace B

both namespaces have class x in each one separately , when we use A.x then there might not be any Naming Convention.

How Assemblies are stored in GAC?

By using Framework they are stored in GAC,those are predefine Assemblies.

Assemblies are of 3 types


1.Private Assembly:Only Application A can use, No other application can access ,usually it will be there in local application /bin folder,that's why other applications can't access.
2.Shared Assembly:It is stored in GAC(Global Assembly Cache) any  of applications can access
3.Satellite Assembly:It will use Culture and Multi Language Assemblies

Assembly in .NET

Deployment unit of Application, dll's,.exe's are called Assemblies.

Ex:System.IO

GAC:Global Assembly Cache (We can see in C:Windows\assembly)

There is a repository there we can store assembly, those assembly we can use for multiple applications.