Csharp/CSharp Tutorial/struct/struct definition

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Composite Struct

<source lang="csharp">struct StructWithNoMembers { }

struct StructWithMembers {

   short s;
   int i;
   long l;
   bool b;

}

struct CompositeStruct {

   StructWithNoMembers a;
   StructWithMembers b;
   StructWithNoMembers c;

}

class SizeofCustomTypesApp {

   unsafe public static void Main(string[] args) 
   {
       Console.WriteLine("sizeof StructWithNoMembers structure = {0}", sizeof(StructWithNoMembers));
       Console.WriteLine("sizeof StructWithMembers structure = {0}",sizeof(StructWithMembers));
       Console.WriteLine("sizeof CompositeStruct structure = {0}", sizeof(CompositeStruct));
   }

}</source>

Create A Struct Using Default Constructor

<source lang="csharp">using System;

   public struct Location
   {
       public int X { get; set; }
       public int Y { get; set; }
       public Location(int x, int y)
           : this()
       {
           X = x;
           Y = y;
       }
       public override string ToString()
       {
           return (String.Format("{0}, {1}", X, Y));
       }
   }
   public class Tester
   {
       static void Main()
       {
           Location loc1 = new Location();  // no call to the constructor
           loc1.X = 75;
           loc1.Y = 225;
           Console.WriteLine(loc1);
       }
   }</source>

Declaration of a struct and use it

<source lang="csharp">using System; public struct MyStruct {

 public int MyInt;
 public long MyLong;
 public string MyString;

} class MainClass {

 static void Main(string[] args)
 {
   MyStruct TheStruct;
   TheStruct.MyInt = 0;
   TheStruct.MyLong = 0;
   TheStruct.MyString = "Hello World";
 }

}</source>

Declare a struct

<source lang="csharp">using System; struct Point {

   public Point(int x, int y)
   {
       this.x = x;
       this.y = y;
   }
   public override string ToString()
   {
       return(String.Format("({0}, {1})", x, y));
   }
   
   public int x;
   public int y;

} class MainClass {

   public static void Main()
   {
       Point start = new Point(5, 5);
       Console.WriteLine("Start: {0}", start);
   }

}</source>

Start: (5, 5)

Declare the Rectangle struct

<source lang="csharp">public struct Rectangle {

 // declare the fields
 public int Width;
 public int Height;
 // define a constructor
 public Rectangle(int Width, int Height)
 {
   this.Width = Width;
   this.Height = Height;
 }
 // define the Area() method
 public int Area()
 {
   return Width * Height;
 }

}

class MainClass {

 public static void Main()
 {
   System.Console.WriteLine("Creating a Rectangle instance");
   Rectangle myRectangle = new Rectangle(2, 3);
   System.Console.WriteLine("myRectangle.Width = " + myRectangle.Width);
   System.Console.WriteLine("myRectangle.Height = " + myRectangle.Height);
   System.Console.WriteLine("myRectangle.Area() = " + myRectangle.Area());
 }

}</source>

Creating a Rectangle instance
myRectangle.Width = 2
myRectangle.Height = 3
myRectangle.Area() = 6

Defining struct

<source lang="csharp">struct Angle {

 public Angle(int hours, int minutes, int seconds)
 {
     _Hours = hours;
     _Minutes = minutes;
     _Seconds = seconds;
 }
 public int Hours
 {
     get { return _Hours; }
 }
 private int _Hours;
 public int Minutes
 {
     get { return _Minutes; }
 }
 private int _Minutes;
 public int Seconds
 {
     get { return _Seconds; }
 }
 private int _Seconds;
 public Angle Move(int hours, int minutes, int seconds)
 {
     return new Angle(Hours + hours,Minutes + minutes,Seconds + seconds);
 }

} class Coordinate {

 public Angle Longitude
 {
     get { return _Longitude; }
     set { _Longitude = value; }
 }
 private Angle _Longitude;
 public Angle Latitude
 {
     get { return _Latitude; }
     set { _Latitude = value; }
 }
 private Angle _Latitude;

}</source>

Use enum data type in a struct

<source lang="csharp">using System; enum EmpType : byte {

 Manager = 10,
 Developer = 1,
 Contractor = 100,
 Programmer = 9

} struct EMPLOYEE {

 public EmpType title;
 public string name;
 public short deptID;
 public EMPLOYEE(EmpType et, string n, short d)
 {
   title = et;
   name = n;
   deptID = d;
 }

} class MainClass {

 public static void Main(string[] args) {
   EMPLOYEE fred;
   fred.deptID = 40;
   fred.name = "Fred";
   fred.title = EmpType.Developer;
   EMPLOYEE mary = new EMPLOYEE(EmpType.Programmer, "Mary", 10);
 }

}</source>