using System;
using System.Collections.Generic;
using System.Text;
namespace OOPS
{
/// <summary>
/// Example for a class which is not allowing to create a object or instance by using private constructor.
/// </summary>
public class PrivateConstructor
{
public float num = 0;
/// <summary>
/// If you try to create a object for this class by using default contructor it gives compile error as inaccessible due to its protection level
/// because it is private constructor.
/// </summary>
private PrivateConstructor()
{
// So We can not create object/instace for a class which has private constructor
}
/// <summary>
/// You can create a object for this class by using parameterised contructor because it is public.
/// </summary>
/// <param name="a"></param>
public PrivateConstructor(int a)
{
// Even if you have a privae constructor,you can create object/instance by using public parameterised constructor-
// as default constructor is already declared as private
}
public float Sum(float a, float b)
{
return num= a + b;
}
public static float Multiply(float a, float b)
{
return a * b;
}
}
/// <summary>
/// A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only.
/// It is called automatically before the first instance is created or any static members are referenced.
/// </summary>
public class StaticConstructor
{
/// <summary>
/// public static SingleTonClass() -- Access modifiers are not allowed on Static constructors
/// static SingleTonClass(int a) -- Static constuctor is not parameterised
/// A static constructor does not take access modifiers or have parameters.
/// A static constructor is called automatically to initialize the class before the first instance is created or any static members are called.
/// A static constructor cannot be called directly.
/// The user has no control on when the static constructor is executed in the program.
/// A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
/// Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
/// </summary>
static int a=0;
static StaticConstructor()
{
a=10;
}
public float Sum(float a, float b)
{
return a + b;
}
public static float Multiply(float a, float b)
{
return a * b;
}
public virtual float Division(float a, float b)
{
return a / b;
}
public virtual float Minus(float a, float b)
{
return a - b;
}
// Abstract method doesn't have body.
//public abstract double SquareRoot(float a);
public int GetA()
{
return a;
}
public int Increment()
{
return ++a;
}
}
// Note:
// 1.If Two constructors of base and derive are public then when we create a instance of derive it calls the first base class constructor and then derive class constructor is called.
// 2.If Two constructors of base and derive are static then when we create a instance of derive it calls the first Derive class constructor and then base class constructor is called.
public class DerivedStaticConstructor:StaticConstructor
{
static DerivedStaticConstructor()
{
}
public new float Sum(float a, float b)
{
return a + b + 10;
}
public override float Division(float a, float b)
{
return a / b;
}
public new float Minus(float a, float b)
{
return a - b;
}
//public override double SquareRoot(float a)
//{
// return Math.Sqrt(a);
//}
}
/*
static void main()
{
DerivedStaticConstructor dSC = new DerivedStaticConstructor();
float d= dSC.Sum(2, 3); -- It calls Derived sum function
d = dSC.Division(4, 2); -- It calls Derived Division function
d = dSC.Minus(4, 2); -- It calls Derived Minus Function
StaticConstructor dS = new DerivedStaticConstructor();
d =dS.Sum(2, 3); -- It calls base class Sum function
d =((DerivedStaticConstructor)dS).Sum(2, 3); -- It calls Derived class Sum function
d = dS.Division(4, 2); -- It calls Derived class Division Function becaus it is override function
d = ((StaticConstructor)dS).Division(4, 2); -- It calls Derived class Division Function because it is override function
d = ((DerivedStaticConstructor)dS).Division(4, 2); -- It calls Derived class Division Function because it is override function
d = dS.Minus(4, 2); -- It calls base class Minus function
d = ((DerivedStaticConstructor)dS).Minus(4, 2); -- It calls Derived class Minus function because it is typecast to derived class
}
*/
/// <summary>
/// To create a singleTon class we must create private class and create static object of that class and create static method to get that instance
/// </summary>
public class SingleTonClass
{
// Create private static instance of this class.
private static SingleTonClass objSTC = new SingleTonClass();
public float num=0;
/// <summary>
/// Private Class which is not allowed to create instance.
/// </summary>
private SingleTonClass()
{
}
public float Sum(float a, float b)
{
return num=a + b;
}
public static float Multiply(float a, float b)
{
return a * b;
}
/// <summary>
/// Static function returns the only one instance of this class which is created as static object
/// User should call this function to get the instance, as this class is not allowed to create instance using 'New' keyword.
/// </summary>
/// <returns></returns>
public static SingleTonClass GetInstance()
{
if (objSTC == null)
objSTC = new SingleTonClass();
return objSTC;
}
}
/// <summary>
/// If two partial class are created with the same name, then we can access the all methods of two classes.
/// </summary>
public partial class IamPartial
{
int number = 0;
public void SetValue(int n)
{
number = n;
}
public int GetValue()
{
return number;
}
}
public partial class IamPartial
{
public float Sum(float a, float b)
{
return a + b;
}
public float Multiply(float a, float b)
{
return a * b;
}
}
/*
public static Main()
{
IamPartial objP = new IamPartial();
objP.SetValue(5); -- Belongs to first partial class with the same instance/object.
float value=objP.GetValue(); -- Belongs to first partial class with the same instance/object.
value=objP.Sum(2, 3); -- Belongs to second partial class with the same instance/object.
value = objP.Multiply(2, 3); -- Belongs to second partial class with the same instance/object.
}
*/
}