Structure is a value type object in.Net framework. Structure is a composite type which can hold element, property, method, constructor, operator in it. Structure is a combination of element to hold a particular composite data.
For example you want to represent Student data. The minimum requirement of the student data is
- Student name
- Student roll number.
Here is the example of how a student structure is defined.
Example
using System;
namespace
student
{
public
struct
Student
{
public
string
name ;
public
int
roll_number ;
public
double
GetFees(string
name, int
roll_number)
{
//statement
return
0.0;
}
}
class
student
{
static
void
main(string[]
arg)
{
Student
obj = new
Student();
obj.name
= "John";
obj.roll_number
= 100;
}
}
}
Here are some important points related to structure
1) struct keyword is used to represent a structure .
2) You need to create an instance of the structure to use it.
3) Structure can implement interface.
4) Structure is value type.
5) Structure can have method field property index method overloading operator overloading events
6) new keyword is used to create a instance of a structure.
7)Structure do not inherit another structure. That means , structure does not support inheritance.
Example 2
using
System;
namespace
student
{
interface
school
{
int
GetTotalMarks(int
roll_number);
}
public
struct
Student
: school
{
private
string
name ;
private
int
roll_number;
//structure implement property
public
string
Name
{
get
{
return
name;
}
set
{
name
= value;
}
}
public
int
Roll_number
{
get
{
return
roll_number;
}
set
{
roll_number
= value;
}
}
//Method
Declaration of Interface
public
double
GetTotalMarks(int
roll_number)
{
//statement
return
0.0;
}
//Method
Declaration : Custom Method
public
double
GetFees(string
name, int
roll_number)
{
//statement
return
0.0;
}
//Method
Declaration : Method Overloading
public
double
GetFees(string
name)
{
//statement
return
0.0;
}
}
class
student
{
static
void
main(string[]
arg)
{
Student
obj = new
Student();
obj.Name
= "John";
obj.Roll_number
= 100;
Double
GetFees = obj.GetFees(obj.Name, obj.Roll_number);
Console.WriteLine("Fees
is :"
+ GetFees.ToString());
Double
GetTotalMarks = obj.GetTotalMarks(obj.Roll_number);
Console.WriteLine("Total
Marks is :"
+ GetTotalMarks.ToString());
}
}
}
You do not need to declare constructor for a structure .By default structure contain constructor. But structure constructor can be defined only for passing parameter to the structure variable. The constructor of a structure also can be overloaded
using
System;
namespace student
{
public
struct
Student
{
private
string
name ;
private
int
roll_number;
public
Student(string
Xname,int
Xroll_number)
{
name=Xname;
roll_number=Xroll_number;
}
public
string
GetName()
{
return
name;
}
public
int
GetRollNumber()
{
return
roll_number;
}
}
class
student
{
static
void
main(string[]
arg)
{
Student
obj = new
Student("John",100);
Console.WriteLine("Fees
is :"
+ obj.GetName().ToString());
Console.WriteLine("Fees
is :"
+ obj.GetRollNumber().ToString());
}
}
}
No comments:
Post a Comment