2011-06-14 71 views
11

我有下面的類:C#類的構造函數默認值的問題

public class Topic 
    { 
     public string Topic { get; set; } 
     public string Description { get; set; } 
     public int Count { get; set; } 
    } 

我想有當與下面是創建類伯爵總是被設置爲零:

var abc = new Topic { 
    Topic = "test1", 
    Description = "description1" 
} 

我與構造函數有點混淆。當我創建abc時,這是可能的還是需要指定主題,描述和計數?

回答

11

您有幾個不同的選項。

1)int默認爲零,所以如果您不初始化它將爲零。

2)你可以使用一個構造函數

public Topic(){ Count = 0;} 

3)您可以使用支持字段,而不是自動財產和初始化爲零

private int _count = 0; 
public int Count { 
    get {return _count} 
    set {_count = value; } 
} 
15

一個int的缺省值是0。

所有值類型具有默認值,因爲它們不能null

請參閱此MSDN頁面上的Initializing Value Types

+0

這是個好消息F或者我。如何字符串和布爾。他們是否默認爲「」和false? – Geraldo 2011-06-14 12:58:37

+0

@Geraldo - 編號字符串是引用類型,所以它們默認爲'null'。 – Oded 2011-06-14 12:59:27

+0

有沒有一種方法可以將字符串設置爲默認值,還是必須指定每個字符串? – Geraldo 2011-06-14 13:00:36

7

Count將在初始化時默認爲0,因爲它是值類型,不能是null

0

編輯

正如我從這個答案的評論中學到的,在初始化器調用中忽略()是完全有效的。

正確的語法是 我希望的語法仍然是:

var abc = new Topic() { 
    Topic = "test1", 
    Description = "description1" 
} 

(注意())。

這會將Count初始化爲0,因爲0是int的默認值。如果你想始終指定主題和描述,添加一個顯式的構造函數:

public Topic(string topic, string description) 
{ 
    Topic = topic; 
    Description = description; 
    // You may also set Count explicitly here, but if you want "0" you don't need to 
} 
+1

-1:關於'()'沒有什麼需要注意的。 – leppie 2011-06-14 13:02:54

+0

咦? OP沒有在他的「構造函數調用」中列出一個空的參數列表 - 我向他指出了這一點,因爲我無法使源代碼大膽,所以注意到我的變化,以便他更容易看到。那給了我一個-1? – 2011-06-14 13:08:17

+0

@Thorsten Dittmar:不管你在語法中是否有'()',它都沒有任何區別。檢查IL。 – leppie 2011-06-14 13:12:31

4

這下面的語句不僅是一個構造函數:

var abc = new Topic { 
    Topic = "test1", 
    Description = "description1" 
} 

這是一個構造函數和一個對象初始化。

真正發生的是new Topic()被首先調用,因此將所有值初始化爲它們的默認值(屬性Topic爲null,說明爲空,Count爲0)。之後,將值「test1」分配給Topic,並將值「description1」分配給Description。

所有值類型都具有不同於null的默認值(因爲它們不能爲空),並且引用類型默認爲null。

0

公共類節目 { 公共靜態無效的主要() {

// Declare a StudentName by using the constructor that has two parameters. 
    StudentName student1 = new StudentName("Craig", "Playstead"); 

    // Make the same declaration by using a collection initializer and sending 
    // arguments for the first and last names. The default constructor is 
    // invoked in processing this declaration, not the constructor that has 
    // two parameters. 
    StudentName student2 = new StudentName 
    { 
     FirstName = "Craig", 
     LastName = "Playstead", 
    }; 

    // Declare a StudentName by using a collection initializer and sending 
    // an argument for only the ID property. No corresponding constructor is 
    // necessary. Only the default constructor is used to process object 
    // initializers. 
    StudentName student3 = new StudentName 
    { 
     ID = 183 
    }; 

    // Declare a StudentName by using a collection initializer and sending 
    // arguments for all three properties. No corresponding constructor is 
    // defined in the class. 
    StudentName student4 = new StudentName 
    { 
     FirstName = "Craig", 
     LastName = "Playstead", 
     ID = 116 
    }; 

    System.Console.WriteLine(student1.ToString()); 
    System.Console.WriteLine(student2.ToString()); 
    System.Console.WriteLine(student3.ToString()); 
    System.Console.WriteLine(student4.ToString()); 
} 

// Output: 
// Craig 0 
// Craig 0 
// 183 
// Craig 116 

}

公共類StudentName {

// The default constructor has no parameters. The default constructor 
// is invoked in the processing of object initializers. 
// You can test this by changing the access modifier from public to 
// private. The declarations in Main that use object initializers will 
// fail. 
public StudentName() { } 

// The following constructor has parameters for two of the three 
// properties. 
public StudentName(string first, string last) 
{ 
    FirstName = first; 
    LastName = last; 
} 

// Properties. 
public string FirstName { get; set; } 
public string LastName { get; set; } 
public int ID { get; set; } 

public override string ToString() 
{ 
    return FirstName + " " + ID; 
} 

}

+0

KINDLY USE MSDN http://msdn.microsoft.com/en-us/library/bb397680的.aspx#Y255 – amod 2011-06-14 13:16:09