DIFFERENT KIND OF VARIABLE OF A CLASS
kind of variable consist in a class |
You should
know about variable before its kind so.
“Variable
is a name of memory storage which we used to store a value.”
Whenever we a have a value of any nature then we need a
memory space from where we can store our value thus the concept of variable
come into play at this time we declare a storage space to store that value the
memory location are called as variable.
We can
differ its kind according its size and behavior according to this in c# we have
four main kind of variable that are.
1. Non-static variable
2. Static variable
3. Constant variable
4. Read-Only variable
STATIC VARIABLE
There are two possibilities to a variable to be a static.
Case 1
If a variable is explicitly declare using the keyword/modifier static then that variable will and must be a static variable like this.
static int x; // here this x is a static variable because its define using static
Keyword.
Case 2
The second case for a variable to be static is if any variable define under a static class or static block then also that variable is known as static variable. Such as suppose Test () is a static method then,
public static void Test ( )
{
int x; // here the variable x also static because of the static block.
}
- The static variable can be call without instantiating its instance or object in any class of the project. We can just directly call them in any class, because it’s initializing just after deceleration implicitly by default values just after the program execute, but yes you can change it explicitly.
- In the life cycle of a class the static variable initialize only one time. And allocate memory location immediately just after a class execute.
- Static variable in another word it have a fixed value so when we
need not to create instance/object to initialized the variable thus we can
change this value during creating instance.
NON STATIC VARIABLE
All the
variable that declare without using a static modifier and the variable must not
declare under a static class or block are known as non-static variable.
Non-static variable is also known as instance. Under normal class and block we
declare a variable without any modifier will also non-static such as.
int x;
public void
Test ( )
{
Int x; // non-static variable
due to non-static block
}
- Non-static variable can call in any class by creating its
instance in that class. Whenever we create instance the variable initialize by
default value it is changeable.
- Non-static variable initialize zero time if no instance are
created and ‘n’ time if n instances are created. Every time of creating
instance a copy of memory location is allocated for the variable in memory.
- Non-static variable can be initialized through the constructor
during instantiating object. Because its associated with creating instance.
CONSTANT VARIABLE
“If any variable is declared by using
keyword ‘const’ we call that variable as constant variable.”
const datatype name = initailzed;
- It’s understood by the name that it’s a constant variable thus
we cannot modified/change once after its declaration. Thus the value must be defined during
declaration of this type of variable. Such as
const float
pi= 3.14f; // for the float data type
its mandatory to use f as suffix in c#
- In life cycle constant variable initialize one time so it has
only one memory like static variable,
- Like static it also no require of instance creating to accessing
and initializing.
- The difference between static and constant variable is only one
that this static variable can be modified and constant variable cannot be
modified.\
READ-ONLY VARIABLE
“The
variable that is declared using readonly keyword is called as readonly
variable.” This variable cannot modify
after initialized.
readonly datatype <name>;
- For the readonly variable it is not require initializing during declaration.
But once it’s declared it cannot be change. It can initialize under a constructor
of class.
Like suppose Test ( ) is a class then,
readonly bool flag;
public void class ( )
{
this.flag = false;
}
- The behavior is similar to non-static so it can have ‘n’ copies
of the readonly variables, because it initialized after creating the instance n
time of creating instance a copy of this variable have.
- The readonly variable can initialize in the constructor of class
or during creating instance, not after this not first.
0 comments:
Post a Comment
Please do not enter any spam link in the comment box