Ad Code

Responsive Advertisement

Constant Variables & ReadOnly Variables

Constant variables – 

These are variables which hold a fixed value that cannot be modified throughout the class. The behavior of constant variables will be similar to static variables but a static variable can be modified and constant variable cannot be modified.

ReadOnly variables – 

These are also variables which hold a fixed value but specific to each object of the class these variable behavior will be similar to instance variables where as an instance variable can be modified and readonly variable cannot be modified.

In case of constants initialization of the variable should be done at the time of declaration only , but in case of ReadOnly initialization can be done either while declaration or under a constructor also.

- Add a class StatVars.cs and write the following –
using System;
namespace oopsProject
{
class StatVars
{
int x = 100; //instance
static int y = 200; //static
public const float pi = 3.14f;
public readonly bool flag;
public StatVars(int x, bool flag)
{
this.x = x;
this.flag = flag;
}
static void Main()
{
Console.WriteLine(StatVars.y);
Console.WriteLine(StatVars.pi);
StatVars obj1 = new StatVars(100,false );
StatVars obj2 = new StatVars(1000, false);
Console.WriteLine(obj1.x + " "+obj2.x);
Console.WriteLine(obj1.flag + " " + obj2.flag);
Console.ReadLine();
}
}
}



Static Methods VS Instance Method –

       1. A method that is declared using a static modifier is a static method rest of all was instance only.
       2. Each and every thing we have discussed in case of variables apply to method also, but while writing               static methods we need to considered an important rule i.e. non-static members of a class cannot be             consumed from static blocks directly. They need to be referred only by object of that class.

- add class StatMets.cs and write the following:
using System;
namespace oopsProject
{
class StatMets
{
int x = 100;
static int y =200;
static void ADD()
{
StatMets obj = new StatMets ();
Console.WriteLine(obj.x +y);
}
static void Main()
{
StatMets.ADD();
Console.ReadLine();
}
}
}

Static Constructor VS Instance Constructor –

      1. A constructor which is declared by using static modifier is a Static Constructor, rest of all were instance      only.
       2. An Instance Constructor is responsible in initializing instance variable of a class whereas Static                       Constructor will initialize static variables.
        3. A Static Constructor is the first block of code which executes under a class whereas an instance n                  constructor is executed each time the object of a class is created.
         4. An instance constructor can be parameterized whereas a Static constructor cannot be parameterized           because we never call a Static constructor directly. So sending values to the parameters will not be                possible...

- Add a class StatCon.cs and Write the following:
using System;
namespace oopsProject
{
class StatCon
{
static StatCon()
{
Console.WriteLine("Static Constructor");
}
StatCon()
{
Console.WriteLine("Instance method");
}
static void Main()
{
Console.WriteLine("Main method");
StatCon obj1 = new StatCon();
StatCon obj2 = new StatCon();
Console.ReadLine();
}
}
}

STATIC CLASSES :-

It is a class which can contain only STATIC members in it. As it contains only static members we cannot create the Object of Static Class.

INHERITANCE –

According properties of parent by children is known as inheritance. So if parent/child relationship can be established between classes, members that are defined in one class can be consumed by the child classes as if they were its own members.

Note – Private members of a class cannot be acquired by its child classes as the default scope of members in a class is private they can not be consumed under child classes.
[<modifier>]class <CCNAME>:<PC NAME>
{
- stmts
}

Class Class1
{
-Members
}
class Class2:Class1
{
-Consume members of Parent
}


Reactions

Post a Comment

0 Comments