• Structure


    Fundamentals of Structures

    代码
    struct Integer
    {
        
    private int val;

        
    public int Value
        {
            
    get { return val; }
            
    set { val = value; }
        }

        
    public int Read()
        {
            
    return int.Parse(Console.ReadLine());
        }
    }


    Structure Declaration
    代码
    using System;

    struct Integer
    {
        
    private int val;

        
    public int Value
        {
            
    get { return val; }
            
    set { val = value; }
        }

        
    public int Read()
        {
            
    return int.Parse(Console.ReadLine());
        }
    }

    class Program
    {
        
    static int Main()
        {
            var Natural 
    = new Integer();

            Console.Write(
    "Enter a natural number: ");
            
    // Accessing a property of the structure
            Natural.Value =
            
    // Calling a method of the structure
            Natural.Read();

            Console.WriteLine(
    "The value you entered was: {0}", Natural.Value);
        
    return 0;
        }
    }

    Here 
    is an example of running the program:

    Enter a natural number: 
    248
    The value you entered was: 
    248
    Press any key to 
    continue . . .






    Techniques of Using Structures 

    A Structure as a Property

    代码
    using System;

    public struct Real
    {
        
    private double val;

        
    public double Value
        {
            
    get { return val; }
            
    set { val = value; }
        }

        
    public double Read()
        {
            
    return double.Parse(Console.ReadLine());
        }
    }

    public struct Rectangle
    {
        Real len;
        Real hgt;

        
    public Real Length
        {
            
    get { return len; }
            
    set { len = value; }
        }

        
    public Real Height
        {
            
    get { return hgt; }
            
    set { hgt = value; }
        }

        
    public void CreateRectangle()
        {
            Real rat 
    = new Real();

            Console.WriteLine(
    "Enter the dimensions of the rectangle");
            Console.Write(
    "Enter the length: ");
            len.Value 
    = rat.Read();
            Console.Write(
    "Enter the height: ");
            hgt.Value 
    = rat.Read();
        }
    }

    public class Program
    {
        
    static int Main()
        {
            var Rect 
    = new Rectangle();

            Rect.CreateRectangle();
            Console.WriteLine();

            Console.WriteLine(
    "Rectangle Characteristics");
            Console.WriteLine(
    "Length:    {0}", Rect.Length.Value);
            Console.WriteLine(
    "Height:    {0}", Rect.Height.Value);
            Console.WriteLine(
    "Perimeter: {0}",
                (Rect.Length.Value 
    + Rect.Height.Value) * 2);
            Console.WriteLine(
    "Area:      {0}",
                Rect.Length.Value 
    * Rect.Height.Value);

            
    return 0;
        }
    }

    Here 
    is an example of running the program:

    Enter the dimensions of the rectangle
    Enter the length: 
    44.84
    Enter the height: 
    26.75

    Rectangle Characteristics
    Length:    
    44.84
    Height:    
    26.75
    Perimeter: 
    143.18
    Area:      
    1199.47
    Press any key to 
    continue . . .


     

    Returning a Structure From a Method 

    代码
    using System;

    public struct Real
    {
        
    private double val;

        
    public double Value
        {
            
    get { return val; }
            
    set { val = value; }
        }

        
    public double Read()
        {
            
    return double.Parse(Console.ReadLine());
        }
    }

    public struct Rectangle
    {
        Real len;
        Real hgt;

        
    public Real Length
        {
            
    get { return len; }
            
    set { len = value; }
        }

        
    public Real Height
        {
            
    get { return hgt; }
            
    set { hgt = value; }
        }

        
    public void CreateRectangle()
        {
            Real rat 
    = new Real();

            Console.WriteLine(
    "Enter the dimensions of the rectangle");
            len 
    = GetLength();
            Console.Write(
    "Enter the height: ");
            hgt.Value 
    = rat.Read();
        }

        
    public Real GetLength()
        {
            Real rat 
    = new Real();

            Console.Write(
    "Enter the length: ");
            rat.Value 
    = rat.Read();
            
    return rat;
        }
    }

    public class Program
    {
        
    static int Main()
        {
            var Rect 
    = new Rectangle();

            Rect.CreateRectangle();
            Console.WriteLine();

            Console.WriteLine(
    "Rectangle Characteristics");
            Console.WriteLine(
    "Length:    {0}", Rect.Length.Value);
            Console.WriteLine(
    "Height:    {0}", Rect.Height.Value);
            Console.WriteLine(
    "Perimeter: {0}",
                (Rect.Length.Value 
    + Rect.Height.Value) * 2);
            Console.WriteLine(
    "Area:      {0}",
                 Rect.Length.Value 
    * Rect.Height.Value);

            
    return 0;
        }
    }

    Here is an example of running the application:

    Enter the dimensions of the rectangle
    Enter the length: 36.04
    Enter the height: 22.86

    Rectangle Characteristics
    Length:    36.04
    Height:    22.86
    Perimeter: 117.8
    Area:      823.8744
    Press any key to continue . . .

    Passing a Structure as Argument 

    代码
    using System;

    public struct Real
    {
        
    private double val;

        
    public double Value
        {
            
    get { return val; }
            
    set { val = value; }
        }

        
    public double Read()
        {
            
    return double.Parse(Console.ReadLine());
        }
    }

    public struct Rectangle
    {
        Real len;
        Real hgt;

        
    public Real Length
        {
            
    get { return len; }
            
    set { len = value; }
        }

        
    public Real Height
        {
            
    get { return hgt; }
            
    set { hgt = value; }
        }

        
    public void CreateRectangle()
        {
            Real rat 
    = new Real();

            Console.WriteLine(
    "Enter the dimensions of the rectangle");
            len 
    = GetLength();
            Console.Write(
    "Enter the height: ");
            hgt.Value 
    = rat.Read();
        }

        
    public Real GetLength()
        {
            Real rat 
    = new Real();

            Console.Write(
    "Enter the length: ");
            rat.Value 
    = rat.Read();
            
    return rat;
        }
    }

    public class Program
    {
        
    public static void ShowCharacteristics(Rectangle rect)
        {
            Console.WriteLine(
    "Rectangle Characteristics");
            Console.WriteLine(
    "Length:    {0}", rect.Length.Value);
            Console.WriteLine(
    "Height:    {0}", rect.Height.Value);
            Console.WriteLine(
    "Perimeter: {0}",
            (rect.Length.Value 
    + rect.Height.Value) * 2);
            Console.WriteLine(
    "Area:      {0}",
            rect.Length.Value 
    * rect.Height.Value);
        }
        
    static int Main()
        {
            var r 
    = new Rectangle();
            Rect.CreateRectangle();
            
            Console.WriteLine();
            ShowCharacteristics(r);

            
    return 0;
        }
    }

    Here is an example of running the program:

    Enter the dimensions of the rectangle
    Enter the length: 114.55
    Enter the height: 82.72

    Rectangle Characteristics
    Length: 114.55
    Height: 82.72
    Perimeter: 394.54
    Area: 9475.576
    Press any key to continue . . .

     Reference: http://www.csharpkey.com/csharp/builtinclasses/structures.htm

     

  • 相关阅读:
    GUI设计禁忌 之三 ——没有指示用户当前所在位置
    GUI设计禁忌 之九 ——不好的布局和窗口位置
    GUI设计禁忌 之十 ——字体过小
    GUI设计禁忌 之五 ——糟糕的搜索引擎
    Oracle对表的基本操作
    GUI设计禁忌 之八 ——引起误解的文字
    GUI设计禁忌 之四 ——将用户引入歧途
    Java基础:Object类中的equals与hashCode方法
    为应用程序池提供服务的进程关闭时间超过了限制
    asp.net 新手资源教程《Total Training Visual Studio 2005 教程》[Microsoft Visual Studio 2005 Productivity with .NET Framework 2.0]【ISO】
  • 原文地址:https://www.cnblogs.com/sandy_liao/p/1891483.html
Copyright © 2020-2023  润新知