Lập trình C# - Mảng struct

Sử dụng mảng struct

Phát triển bài tập tạo một Book Struct, hay tạo NhanVien Struct, giả sử bây giờ chúng cần lưu giữ thông tin của hàng ngàn cuốn sách hay thông tin của hàng trăm nhân viên, thì bây giờ chúng ta làm như thế nào. Vấn đề này sẽ được giải quyết bởi sử dụng mảng struct trong C#.

Chương trình C#

Dưới đây là chương trình C# minh họa lời giải cho bài tập sử dụng mảng struct trong C#:

using System;

namespace Csharp
{
    public class TestCsharp
    {
        //khai bao mot struct
        struct point
        {
            //toa do cua diem
            public short x;
            public short y;

            //mau cua diem
            public byte r;
            public byte g;
            public byte b;
        }

        public static void Main()
        {
            point[] p = new point[1000];

            Console.Write("Vi du minh hoa mang struct trong C#:\n");
            Console.Write("-----------------------------------------\n");

            Console.Write("Nhap toa do X cho diem dau tien: ");
            p[0].x = Convert.ToInt16(Console.ReadLine());

            Console.Write("\nNhap toa do Y cho diem dau tien: ");
            p[0].y = Convert.ToInt16(Console.ReadLine());

            Console.Write("\nNhap mau do cho diem dau tien: ");
            p[0].r = Convert.ToByte(Console.ReadLine());

            Console.Write("\nNhap mau xanh la cay cho diem dau tien: ");
            p[0].g = Convert.ToByte(Console.ReadLine());

            Console.Write("\nNhap mau xanh da troi cho diem dau tien: ");
            p[0].b = Convert.ToByte(Console.ReadLine());


            Console.Write("\nNhap toa do X cho diem thu hai: ");
            p[1].x = Convert.ToInt16(Console.ReadLine());

            Console.Write("\nNhap toa do Y cho diem thu hai: ");
            p[1].y = Convert.ToInt16(Console.ReadLine());

            Console.Write("\nNhap mau do cho diem thu hai: ");
            p[1].r = Convert.ToByte(Console.ReadLine());

            Console.Write("\nNhap mau xanh la cay cho diem thu hai: ");
            p[1].g = Convert.ToByte(Console.ReadLine());

            Console.Write("\nNhap mau xanh da troi cho diem thu hai: ");
            p[1].b = Convert.ToByte(Console.ReadLine());

            //... ban co the nhap n diem

            Console.WriteLine(
                "Diem thu nhat tai vi tri ({0},{1}), co mau ({2},{3},{4})",
                p[0].x, p[0].y, p[0].r, p[0].g, p[0].b);

            Console.WriteLine(
                "Diem thu hai tai vi tri ({0},{1}), co mau ({2},{3},{4})",
                p[1].x, p[1].y, p[1].r, p[1].g, p[1].b);

            Console.ReadKey();
        }
    }
}

Xem ví dụ

Biên dịch và chạy chương trình C# trên sẽ cho kết quả:

Vi du minh hoa mang struct trong C#:
-----------------------------------------
Nhap toa do X cho diem dau tien: 10

Nhap toa do Y cho diem dau tien: 10

Nhap mau do cho diem dau tien: 20

Nhap mau xanh la cay cho diem dau tien: 20

Nhap mau xanh da troi cho diem dau tien: 2

Nhap toa do X cho diem thu hai: 1

Nhap toa do Y cho diem thu hai: 3

Nhap mau do cho diem thu hai: 5

Nhap mau xanh la cay cho diem thu hai: 6

Nhap mau xanh da troi cho diem thu hai: 3
Diem thu nhat tai vi tri (10,10), co mau (20,20,2)
Diem thu hai tai vi tri (1,3), co mau (5,6,3)