程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 類與結構的區別

C# 類與結構的區別

編輯:C#入門知識

類與結構的區別
C++中結構與類的唯一區別是在於默認的訪問級別,但是C#中的區別就稍微多了點,C#中如下
(1)類是引用類型,結構是值類型。
類中:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}


class Program
{
    static void Main()
    {
        Person person1 = new Person("Leopold", 6);
        Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age);


        // Declare  new person, assign person1 to it.
        Person person2 = person1;


        //Change the name of person2, and person1 also changes.
        person2.Name = "Molly";
        person2.Age = 16;


        Console.WriteLine("person2 Name = {0} Age = {1}", person2.Name, person2.Age);
        Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age);


        // Keep the console open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();


    }
}


Output:
person1 Name = Leopold Age = 6
person2 Name = Molly Age = 16
person1 Name = Molly Age = 16

結構中:

public struct Person
{
    public string Name;
    public int Age;
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

public class Application
{
    static void Main()
    {
        // Create  struct instance and initialize by using "new".
        // Memory is allocated on thread stack.
        Person p1 = new Person("Alex", 9);
        Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age);


        // Create  new struct object. Note that  struct can be initialized
        // without using "new".
        Person p2 = p1;


        // Assign values to p2 members.
        p2.Name = "Spencer";
        p2.Age = 7;
        Console.WriteLine("p2 Name = {0} Age = {1}", p2.Name, p2.Age);


        // p1 values remain unchanged because p2 is  copy.
        Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age);


        // Keep the console open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
Output:
p1 Name = Alex Age = 9
p2 Name = Spencer Age = 7
p1 Name = Alex Age = 9
(2)結構中可以包含構造函數,但是構造函數必須對全部字段賦值,因此結構沒有默認構造函數
(3)結構不能被繼承,但是結構可以實現接口
(4)結構直接繼承自 System.ValueType,類繼承自 System.Object
(5)結構中除非是const或者static變量,或者不能直接對變量初始化
namespace ConsoleApplication5
{
    struct struct1
    {
        //int x = 10;		//error
        int x;
        string str;


        //struct1(){}		//error


        //struct1(int _x)	//error
        //{ x = _x; }


        struct1(int _x, string _str)
        {
            x = _x;
            str = _str;
        }


        const int y = 10;	//OK
        static int z = 100;	//OK
    }
}

(6)結構可以是Nullable類型
所謂的Nullable類型就是System.Nullable的實例,它的取值范圍是值類型T可以表達的所有有效值 外加一個null值。
Nullable存在的意義:值類型必須初始化後才能使用,為了讓值類型不用初始化,就誕生了這個類型了。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved