0
What is a structure?
help
2 ответов
+ 6
A structure is like a box, you place functions, varibles ,etc inside. When you need to use the box, the system will 'duplicate' that box for you to use it. Convinent if you are going to a certain set of same task for mutliple times in your code.
+ 1
structs are similar to classes in terms of their ability to contain fields, methods and constructors. However structs are NOT classes, they're value types whereas classes are reference types. You'd like to use structs to encapsulate a small set of data. Actually ints,double, float, long etc are structures. Below shows an example of struct.
struct Time {
private int hour;
private int minutes;
private int seconds;
public Time(int hr, int mins, int Seconds){
hour = hr;
minutes = mins;
seconds = Seconds;
}
public override string ToString(){
return hour +":"+ minutes +":"seconds;
}
}
you can use it like this:
Time now = new Time(13,50,34);
Console.Write(now);
// keep in mind even though i am using "new " keyword here, it is NOT an object , it's still a value type stored in stack memory.