C#結構體

來源:互聯網
上載者:User

 

結構體的定義:

結構體也可以象類一樣可以單獨定義.
class  a{};
struct a{};

結構體也可以在名字前面加入控制訪問符.
public struct student{};
internal struct student{};

如果結構體student沒有publice或者internal的聲明 類program就無法使用student結構定義 obj對象
如果結構體student的元素沒有public的聲明,對象obj就無法調用元素x
因為預設的結構體名和元素名是private類型
程式:
using System;
public struct student
    {
       public int x;
    };

class program
{
    public static void Main()
    {
     student obj=new student();
     obj.x=100;       
    }

};
在結構體中也可以定義靜態成員與類中一樣,使用時必須用類名,或結構名來調用不屬於執行個體,聲明時直接定義.
程式:
using System;
public struct student
    {
     public static int a = 10;
    };
class exe
{
 public static void Main()
    {
     Console.WriteLine( student.a = 100);
    }
};

using System;
class base
{
public struct student
    {
     public static int a = 10;
    };
}
class exe
{
 public static void Main()
    {
     Console.WriteLine( base.student.a = 100);
    }
};
在結構體中可以定義建構函式以初始化成員,但不可以重寫預設無參建構函式和預設無參解構函式
程式:
public struct student
    {
       public int x;
       public int y;
       public static int z;
       public student(int a,int b,int c)
        {
            x=a;
            y=b;
         student.z=c;
        }

    };
在結構體中可以定義成員函數。
程式:
public struct student
    {
       public void list()
            {
Console.WriteLine("這是構造的函數");
            }

     };
結構體的對象使用new運算子建立(obj)也可以直接建立單個元素賦值(obj2)這是與類不同的因為類只能使用new建立對象
程式:
public struct student
    {
       public int x;
       public int y;
       public static int z;
       public student(int a,int b,int c)
        {
            x=a;
            y=b;
         student.z=c;
        }

    };
class program
{
 public static void Main()
{
  student obj=new student(100,200,300);
  student obj2;
  obj2.x=100;
  obj2.y=200;
  student.z=300;
}
}

在使用類對象和函數使用時,使用的是引用傳遞,所以欄位改變
在使用結構對象和函數使用時,是用的是值傳遞,所以欄位沒有改變

程式:
using System;
class class_wsy
{
    public int x;
}
struct struct_wsy
{
    public int x;
}
class program
{
    public static void class_t(class_wsy obj)
    {
        obj.x = 90;
    }
    public static void struct_t(struct_wsy obj)
    {
        obj.x = 90;
    }
    public static void Main()
    {
        class_wsy obj_1 = new class_wsy();
        struct_wsy obj_2 = new struct_wsy();
        obj_1.x = 100;
        obj_2.x = 100;
        class_t(obj_1);
        struct_t(obj_2);
        Console.WriteLine("class_wsy obj_1.x={0}",obj_1.x);
        Console.WriteLine("struct_wsy obj_2.x={0}",obj_2.x);
        Console.Read();
    }
}
結果為:class_wsy obj_1.x=90
       struct_wsy obj_2.x=100

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.