STRUCTURE IN C-SHARP
c# struct |
" Structure is a user define type,
which contain almost all the member that a class have such as in structure it
can contain field, method , constructor, properties and operator method etc".
Where in C language the
structure contain only the fields.
DEFINING A STRUCTURE
It almost similar to a class
having modifiers Structure name and of
course struct it is the keyword to define a structure.
Syntax
[<modifiers>] struct <name>
{
Member definition;
}
The only one difference to defining a class and structure is the keyword
class use class as keyword and structure use struct as keyword to defining a
structure and the rest all same in both.
Instantiating
instance of class and calling process of class and structure is also same we
did it like this
Classname
Obj=new Classname; //instantiating the instance in class
Obj.
CLassmembers //
calling the members of class
This gona same in structure also
like this
Structurename
Obj=new Structurename; //instantiating the instance of structure
Obj.
CLassmembers //
calling the members of structure
Example of structure:
Case 1 (used new keyword to creating instance)
Field x is initialized by`
constructor
namespace
Teststrucrture
{
public struct Airthemetic
{
int x;
public void Add(int a, int b)
{
Console.WriteLine(a + b);
}
public void Sub(int a,int b)
{
Console.WriteLine(a - b);
}
static void Main()
{
Airthemetic a = new
Airthemetic();
a.Add(21, 20);
a.Sub(34, 21);
Console.ReadKey();
}
}
}
|
Case
2(without use new keyword)
Field x is explicitly initialized
namespace
Teststrucrture
{
public struct Airthemetic
{
int x;
public void Add(int a, int b)
{
Console.WriteLine(a + b);
}
public void Sub(int a,int b)
{
Console.WriteLine(a - b);
}
static void Main()
{
Airthemetic a; a.x = 3;
a.Add(21, 20);
a.Sub(34, 21);
Console.ReadKey();
}
}
}
|
difference between class and structure |
Difference between class and structure
Class
|
Structure
|
0 comments:
Post a Comment
Please do not enter any spam link in the comment box