2015-10-02 18 views
1

我碰到過這段代碼,我不知道它是什麼或目的。課後使用{}的目的類型c#

A類

internal class RequestBase<T> 
{ 
    public RequestBase() 
    { 
     ID = Helper.GetNextId().ToString(CultureInfo.InvariantCulture); 
    } 
    public RequestBase(string method, T @params) 
     : this() 
    { 
     Method = method; 
     Parameters = @params; 
    } 

    [DataMember(Name = "id")] 
    public string ID { get; private set; } 

    [DataMember(Name = "method")] 
    public string Method { get; set; } 

    [DataMember(Name = "params")] 
    public T Parameters { get; set; } 
} 

B類

[DataContract] 
internal class AuthenicateRequest 
{ 
    [DataMember(Name = "api_key", IsRequired = true)] 
    public string APIKey { get; set; } 

    [DataMember(Name = "secret_key", IsRequired = true)] 
    public string SecretKey { get; set; } 
} 

因此,這裏是我不理解的部分。

var requestObj = new RequestBase<AuthenicateRequest> 
       { 
       Method = "auth.accessToken", 
       Parameters = new AuthenicateRequest 
         { 
         APIKey = api_key, 
         SecretKey = secret_key 
         } 
       }; 

Q1:在部分參數,如何API_KEY得到傳遞給ClassB的APIKey沒有做ClassB.APIKey = API_KEY?問題2:爲什麼發起Parameters = new AuthenicateRequest {APIKey = api_key,SecretKey = secret_key}而不是參數= new AuthenicateRequest(api_key,secret_key)?

我有更多的問題要問,但我想我最好把它放在一個單獨的帖子。

+1

您應該對象初始化讀了,當例如爲:

MyObject obj = new MyObject("hello") { String2 = "world" }; 

這句法變得非常有用。 https://msdn.microsoft.com/en-us/library/bb384062.aspx和https://msdn.microsoft.com/en-us/library/bb397680.aspx –

回答

5

這些被稱爲對象初始值設定項。它們允許您使用更簡潔的語法設置新構造對象的屬性。它們的行爲與構建新對象完全相同,然後將這些屬性逐個設置爲新語句。

參見:https://msdn.microsoft.com/en-us/library/bb384062.aspx

此:

var requestObj = new RequestBase<AuthenicateRequest> 
       { 
       Method = "auth.accessToken", 
       Parameters = new AuthenicateRequest 
         { 
         APIKey = api_key, 
         SecretKey = secret_key 
         } 
       }; 

完全等同於:

var requestObj = new RequestBase<AuthenicateRequest>(); 
requestObj.Method = "auth.accessToken"; 
requestObj.Parameters = new AuthenticateRequest(); 
requestObj.Parameters.APIKey = api_key; 
requestObj.Parameters.SecretKey = secret_key; 

注意,如果一個構造函數需要的參數,這些仍然必須內指定括號無論哪個語法,例如:

var x = new Foo(someParam) { 
      SomeProperty = "foobar", 
      OtherProperty = 4 
     }; 

其是相同

var x = new Foo(someParam); 
x.SomeProperty = "foobar"; 
x.OtherProperty = 4; 
2

Object and Collection Initializers (C# Programming Guide)

對象初始讓你將值分配給任何可用的字段或在創建時的對象的 屬性,而不必調用 構造接着通過賦值語句的行。對象 初始化器語法使您能夠爲構造函數 指定參數或省略參數(和括號語法)。

樣品等級:

class Cat 
{ 
    public int Age { get; set; } 
    public string Name { get; set; } 
} 

初始化使用對象初始化:

Cat cat = new Cat { Age = 10, Name = "Fluffy" }; 

手冊初始化:

Cat cat = new Cat(); 
cat.Age = 10; 
cat.Name = "Fluffy"; 

爲什麼要使用對象初始化?

  • 有時候沒有其他辦法,例如初始化匿名類型時。
  • 它可以防止構造函數有很多過載
  • 有時它使代碼更具可讀性。
+0

我意識到摘錄是一個報價,但它的值得注意的是,這僅僅是具有默認或無參數構造函數的對象的情況。如果該對象沒有無參數構造函數,則仍然需要指定所需的參數。 –

1

建設像Parameters = new AuthenicateRequest { APIKey = api_key, SecretKey = secret_key }是寫

var param = new Parameters(); 
param.APIKey = api_key; 
param.SecretKey = secret_key; 

這就是所謂的對象初始化

2

您正在使用的功能被稱爲 object initializers只是簡短的形式。

對Q1的回答:這只是對象初始值設定項的本質。你能想象的編譯器翻譯 var request = new AuthenticateRequest { APIKey = api_key, SecretKey = secret_key }; var request = new AuthenticateRequest(); request.APIKey = api_key; request.SecretKey = secret_key;

答到Q2:使用對象初始化,如果你有一個可用的構造適合您的需要是沒有意義的。那麼你應該使用它。

0

使用大括號({,}),而實例化一個對象允許您提供該對象的屬性的值。所以這個代碼:

var authRequest = new AuthenicateRequest 
        { 
        APIKey = api_key, 
        SecretKey = secret_key 
        } 

在功能上等同於這樣:

var authRequest = new AuthenicateRequest(); 
    authRequest.APIKey = api_key; 
    authRequest.SecretKey = secret_key; 

要在此展開,你可以嵌套對象創建如你問題的例子:

var requestObj = new RequestBase<AuthenicateRequest> 
    { 
     Method = "auth.accessToken", 
     Parameters = new AuthenicateRequest 
      { 
       APIKey = api_key, 
       SecretKey = secret_key 
      } 
    }; 

哪個相當於此:

var authRequest = new AuthenicateRequest(); 
    authRequest.APIKey = api_key; 
    authRequest.SecretKey = secret_key; 

    var requestObj = new RequestBase<AuthenicateRequest>(); 
    requestObj.Method = "auth.accessToken"; 
    requestObj.Parameters = authRequest; 

您仍然可以使用任何參數化的構造函數如果對象有他們 - 使用lambda表達式等