static class,static method, static constructor |
STATIC CLASS
“Static classes are those classes which are defined by the keyword static.” The keyword static is placed or writes before class and after public. The static class never instantiate and inherited.
SYNTAX:
Public static class ClassName
{
Do the task;
}
WHY WE USE STATIC CLASS?
We use or create
static class when a fixed value or string is predefined. Also we make the class
static so we don’t need to instantiate any object.
RULES TO DECLARE STATIC CLASS:
Following rule will
apply when created a static class.
1. The class must start from the keyword static.
2. The entire class member must be static.
3. It never instantiate nor inherited.
4. The static class and variable always public.
5. The members are accessible throughout in program code.
6. Static member must be fixed value.
7. Static constructor should make to define the values or string. Constructor
does not need to be public.
8. The variable should static and can be access throughout program
code.
STATIC CLASS CALLING:
Static class does
not instantiate so how can we call it from our main program. It’s pretty simple
we call just by class name, by following with a dot after class name the
constructor or method will write to calling. The example given bellow will clear
this problem.
Classname. Methodname
( );
STATIC METHOD AND
CONSTRUCTOR
“All those method and
constructor that create by using keyword static are known as Static Method or
Static Constructor.”
The Static Method
will be Public whereas the constructor may be public or not.
SYNTAX FOR METHOD:
Public static class Classname
{
Public static
void Methodname ( )
{
Do the
task;
}
}
SYNTAX FOR CONSTRUCTOR:
Public static class Classname
{
Static Classname (
)
{
Do the
task;
}
}
FOR EXAMPLE:
public static class Product
{
public static int Productserial;
public static string Productname;
public static int Productprice;
static Product()
{
Productserial = 112;
Productname ="hp laptop Elite Book 8440p";
Productprice = 1499;
}
public static void getproductdetail()
{
Console.WriteLine("Product
Serail = {0}", Productserial);
Console.WriteLine("Product
Name = {0}", Productname);
Console.WriteLine("Product
Price = {0}", Productprice);
}
}
class Program
{
static void Main(string[] args)
{
Product.getproductdetail();
Console.ReadKey();
}
}
STATIC PROPERTIES
click to continue GO
c# constructor
c# getset
c# method
0 comments:
Post a Comment
Please do not enter any spam link in the comment box