Watch before proceed

10. Class & Object 

Part-11-C# Static class & Partial class Explained

Static Class

A C# static class is a class that can be declare as static keyword. Suppose if we apply static modifier to a class, then we should not inherited the class. A static class can contain static members only. You can‘t create an object for the static class.
Note :-
  1. If you declare any field,method as a non-static field,Method, you will get an error "cannot declare instance members in a static class" .
  2. When you try to create an instance to the static class, it again generates a compile time error, because the static members can be accessed directly with its class name.
  3. The static keyword is used before the class keyword in a class definition to declare a static class.
  4. A static class members are accessed by the class name followed by the member name. 
  5. The methods of the static class can be called using the class name without creating the instance.
  6. we cannot create an object of that class using the new keyword
Syntax :
Example-1


 public static class Demo    {
// Static data members of
        public static string name;
        public static string Address;
// Static method
        public static void Test()
        {
            name = "chandan";
            Address = "Delhi";
        }
    } 
   class Program
    {
         {
                Console.WriteLine("My Name is" + Demo.name);
               Console.WriteLine("My Name is" + Demo.Address);
               Console.ReadLine();
        }
    }

Example :
static class Author {
// Static data members of Author public static string name = "Madhu"; public static string language = "CSharp"; // Static method of Author public static void details() { Console.WriteLine("The details of Author is:"); } } class Program { static void Main(string[] args) { //Calling static method of Author Author.details(); Console.WriteLine("Author name : {0} ", Author.name); Console.WriteLine("Language : {0} ", Author.language ); }
}

Example :2

static class Demo{ // Static method of Author public static void details() { Console.WriteLine("The details of Author is:"); } } // try to inherit class DEMO //it will give error as static // class can't be inherited class Program : Demo{ public static void Main(String[] args) { } }
OUTPUT: Cannot derive from static class

Partial Class

A partial class implement the functionality of particular class into multiple class files and all these files will be combined into one single class file. when the application is compiled on CLR, multiple classes or interfaces or struct with partial keyword will be compiled into single unit or it is considered as single class, interface and structFor CLR it is a single class only but for us it will be two separated file into single unit & to achieve this we'll use  partial Keyword.
Syantax:
          public partial class Program
         {
            //Code
         }
Advantage:
  1. Multiple developers can also work simultaneously like one creating logic and other working on designer part.
  2. It allows us to write partial class, interface, struct and method in two or more separate source files and compile it as a single unit.
  3. Larger classes can be split into smaller classes for easy to understand and maintain.
 public partial class Demo
{
        private int x;
        private int y;
        public Demo(int x, int y)        {
            this.x = x;
            this.y = y;
        }
    }
    public partial class Demo
    {
        public void PrintCoords()
        {
            Console.WriteLine("Coords: {0},{1}", x, y);
            Console.WriteLine("Execute the Second Partial class");
        }
    }
Demo demo=new Demo(10,24);
             demo.PrintCoords();
            Console.WriteLine("Press any key to exit.");
            Console.Read();
        }
    }