Search code examples
c#classcurly-braces

the purpose of using {} after class type c#


I came across this code and i have no idea what it is or what is the purpose.

Class 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; }
}

Class 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; }
}

So here is the part where i don't understand.

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

Q1: In the section Parameters, how does api_key get pass to ClassB APIKey without doing ClassB.APIKey = api_key?

Q2: Why initiate Parameters = new AuthenicateRequest { APIKey = api_key, SecretKey = secret_key } rather than do Parameters = new AuthenicateRequest(api_key, secret_key)?

I have more questions to ask but i think i better put it in a separate post.


Solution

  • These are called object initializers. They allow you to set properties of a newly constructed objects with a more concise syntax. They behave exactly the same as constructing a new object and then setting those properties one by one as new statements.

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

    This:

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

    is exactly equivalent to:

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

    Note that if a constructor requires parameters, these must still be specified inside parentheses no matter which syntax is used, e.g.:

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

    which is the same as

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