Csharp/CSharp Tutorial/struct/Properties

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

Accessing Structures with Properties and Public Members

public struct StructWithPublicMembers {
    public int X;
    public int Y;
}
public struct StructWithProperties {
    private int PrivateX;
    private int PrivateY;
    public int X {
        get {
            return PrivateX;
        }
        set {
            PrivateX = value;
        }
    }
    public int Y {
        get {
            return PrivateY;
        }
        set {
            PrivateY = value;
        }
    }
}

public class MainClass {
    public static void Main() {
        StructWithPublicMembers MembersStruct;
        StructWithProperties PropertiesStruct = new StructWithProperties() ;
        MembersStruct.X = 100;
        MembersStruct.Y = 200;
        PropertiesStruct.X = 100;
        PropertiesStruct.Y = 200;
    }
}

Use property in a struct

using System;
public struct Square
{
   public int Width
   {
      get
      {
         return width;
      }
      set
      {
         width = value;
      }
   }
   public int Height
   {
      get
      {
         return height;
      }
      set
      {
         height = value;
      }
   }
   
   private int width;
   private int height;
}
public class MainClass
{
   static void Main()
   {
      Square sq = new Square();
      sq.Width = 1;
      sq.Height = 1;
   }
}