Thursday, 25 August 2011

Partial Class

      When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.To split a class definition, use the partial keyword modifier.
  • Using the partial keyword indicates that other parts of the class, struct, or interface can be defined within the namespace. All the parts must use the partial keyword. All of the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.
  • If any of the parts are declared abstract, then the entire type is considered abstract. If any of the parts are declared sealed, then the entire type is considered sealed. If any of the parts declare a base type, then the entire type inherits that class.
The following Program describes about Partial classes..



public  partial class coords //use abstract and check...
    {
        private int x;
        private int y;

        //public coords() { }
        public coords(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

    public partial class coords
    {
        public void PrintCo()
        {
            System.Console.WriteLine("CoOrds: {0},{1}", x, y);
        }

    }

    class Program //:coords
    {
        static void Main()
        {
            coords obj = new coords(10, 15);
           // Program obj = new Program();
            obj.PrintCo();
            Console.Read();
        }
    }

Output:
CoOrds: 10,15


No comments:

Post a Comment