Wednesday, July 2, 2014

PARTIAL CLASSES

It is an approach of defining a class in more than one file i.e you can split a class into multiple files.

Parts.cs
Class parts
{
Method1()
Method2();
Method3()
Method4()
}

Part1.cs
Partial Class Parts
{
Method1()
Method2()
}

Part2.cs
Partial Class Parts
{
Method3()
Method4()
}

Partial classes provides the following benefit
Splitting up of huge volume of code into multiple files or separating related code into different files which make easy to manage.
Multiple programmers can write on the same class at a time.


Add a class as Part1.cs and write the following code

 Partial Class Parts
{
Public void Method1()
{
Console.writeline(“Method1”);
}
Public void Method2()
{
Console.writeline(“Method2”);
}
}

Part2.cs

 Partial Class Parts
{
Public void Method3()
{
Console.writeline(“Method3”);
}
Public void Method4()
{
Console.writeline(“Method4”);
}
}

Add a class as TestParts.cs and write the following code
Class TestParts
{
Static void Main()
{
Parts P=new Parts();
P.Method1();
P.Method2();
P.Method3();
P.Method4();
Console.ReadLine();
}

}

Sealed Class and Sealed Method

Sealed class
A class which cannot inherits is called as sealed class
Sealed class classname
{

}
Note: all static classes are equal to static classes.
Sealed class A
{
Public int x=100;
}
Class Demo
{
Public static void Main()
{
A obj=new A();
Console.writeline(obj);
}
}
            
Sealed classes:
1)Can create instances, but cannot inherit
2)Can contain static as well as nonstatic members.

Static classes:
1)Can neither create their instances, nor inherit them
2)Can have static members only.

sealed class and sealed method

1.  A class, which restricts inheritance for security reason is declared, sealed class.
2.  Sealed class is the last class in the hierarchy.
3.  Sealed class can be a derived class but can't be a base class.
4.  A sealed class cannot also be an abstract class. Because abstract class has to provide functionality and here we are restricting it to inherit.
Practical demonstration of sealed class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace sealed_class
{
    class Program
    {
        public sealed class BaseClass
        {
           public void Display()
        {
                        Console.WriteLine("This is a sealed class which can’t  be further inherited");
              }
  }
 
        public class Derived : BaseClass
        {
           // this Derived class can’t inherit BaseClass because it is sealed
        }
   
        static void Main(string[] args)
        {
            BaseClass obj = newBaseClass();
 
            obj.Display();
 
            Console.ReadLine();
        }
    }
}





sealed method

  Sealed method is used to define the overriding level of a virtual method.
  Sealed keyword is always used with override keyword.
  Practical demonstration of sealed method
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace sealed_method
{
    class Program
    {
        public class BaseClass
        {
           
            public virtual void Display()
            {
                Console.WriteLine("Virtual method");
            }
        }
 
       publicclass DerivedClass: BaseClass
        {
          // Now the display method have been sealed and can’t  be overridden
            public overridesealed voidDisplay()
            {
                Console.WriteLine("Sealed method");
            }
        }
 
       //public class ThirdClass : DerivedClass
       //{
 
       //    public override void Display()
       //    {
       //        Console.WriteLine("Here we try again to override display method which is not possible and will give error");
       //    }
       //}
 
        static void Main(string[] args)
        {
 
            DerivedClass ob1 = newDerivedClass();
            ob1.Display();
 
            Console.ReadLine();
        }
    }
}
  

Instance Methods Vs Static Methods

Static methods have no instances. They are called with the type name, not an instance identifier. They are slightly faster than instance methods because of this. Static methods can be public or private.

-A method declared with the help of a "static" modifier is a static method and the rest are instance.

-A static method is invoked with the name of its class. eg: WriteLine is a static method under the class Console which is referred as Console.WriteLine.

-A non-static member of a class cannot be referred from a static block direclty, but still if u want to refer this can be done with the object of the class.

Note: While defining static method never refer to the non-static members with out the object of class


using System;
 
namespace Static_var_and_fun
{
  class number
   {
     // Create static variable
     public static int num;
     //Create static method
     public static void power()
      {
        Console.WriteLine("Power of {0} = {1}", num,                num * num);
        Console.ReadLine();
      }
   }
  class Program
   {
     static void Main(string[] args)
      {
        Console.Write("Enter a number\t");
        number.num = Convert.ToInt32(Console.ReadLine());
        number.power();
      }
   }
}


Static Variable Vs Instance Variable

Instance Variable vs static variable

A variable declared using static modifier or a declare under static block were known as “static variable” rest of all are instance variables

 Eg: 
int x=100;
Static int y=200;
Static void Main()
{
Int z=300;
}

Note: here the instance variable gets initialized whenever the object of class gets created, so the minimum or maximum no of times, it gets initialized will be zero or N.

Whereas as a static variable get initialized once the execution of a class starts. so the minimum and the maximum no of times it gets initialized will be one and only one.

Use instance variable if we want the variable accessible only to the single object

Use static variable if we want the variable to accessible to all objects.

Statvar.cs
Class statvars
{
int x=100;
static int y=200;
static void Main()
{
Console.writeline(statvar.y);
Statvar sv=new statvar();
Console.writeline(sv.x);
Console.readline();

}

Static Class

A class in C sharp declared with static keyword is a C# static class. The static class prevents the object creation and can be used directly without creating any instance of class. A class that is not a static class is used after creating its object with new keyword. But the class that is static is used directly without creating its object


What is the benefit of using static class in C#?
1. It makes program faster and simpler because there is no need to create object of static class.
2. Its function can be directly called followed by the class name so, you can use its function anywhere in your program easily.
3. Its variable can also be accessed directly.

Guideline to using static class C#

1. The static class can contain only static members.
2. The static class cannot be instantiated.
3. The static class is a sealed class so you cannot inherit and override its members.
4. Usually it doesn’t contain constructors however; it is possible to declare a static constructor within static class.



Static class
When a class has been defined as static, it is not creatable using the new keyword , and it can contain only static members or fields

The main feature of static class

One is no object of static class can be created
Static class must contain only static members
We do need to make any instance of this class, all members are accessible with its own name.
Static class can’t implement interface
Static class contains only static constructor.
Static class is implicitly sealed.
Static class cannot have instance constructor

Can have only one constructor without any parameter


This makes program fast and simpler because there is no need to create object of static class.

static class Perl
{
    // Cannot declare instance members in a static class!
    // int _test;
 
    // This is ok.
    public static bool _ok;
 
    // Can only have static methods in static classes.
    public static void Blend()
    {
     Console.WriteLine("Blended");
    }
}
 
Output
 

Blended

public static class Rectangle
{
    public static int CalculateArea(int width, int height)
    {
        return width * height;
    }
}
      

Console.WriteLine("The area is: " + Rectangle.CalculateArea(5, 4));



Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...