1

I'm trying to understand the following code:

  flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
  {
    DataStore = new FileDataStore("Tasks.ASP.NET.Sample.Store"),
    ClientSecretsStream = stream,
    Scopes = new[] { TasksService.Scope.TasksReadonly }
  });

From my understanding, the code between the first and last {...} is the body of an anonymous function. The new FileDataStore creates a new instance of FileDataStore. What I don't understand is what the comma at the end of it means. The two lines following it also have commas at the end. What kind of construct is this called in C#? I'm not familiar with it.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Johann
  • 27,536
  • 39
  • 165
  • 279

3 Answers3

9

No, it isn't the body of an anonymous function. It is an initialization list.. and it serves to set the fields of the new object of type GoogleAuthorizationCodeFlow.Initializer all in-line.

It is the "in-line" version of this:

var initializer = new GoogleAuthorizationCodeFlow.Initializer();
initializer.DataStore = new FileDataStore("Tasks.ASP.NET.Sample.Store");
initializer.ClientSecretsStream = stream;
initializer.Scopes = new[] { TasksService.Scope.TasksReadonly };

flow = new GoogleAuthorizationCodeFlow(initializer);

The two are equivalent functionally. It is just more compact.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • What you wrote above is what I am more familiar with. What version of c# was this inline introduced in? Any idea? I don't recall it being in earlier versions. I actually like the longer method. Easier on the eyes :-) – Johann Jan 08 '14 at 11:02
  • @AndroidDev Object and collection initializers were introduced in [C# 3.0](http://msdn.microsoft.com/en-us/library/bb308966.aspx) (March 2007). – Brian Rogers Jan 09 '14 at 21:26
3

It's a constructor initializer. The code is creating a new GoogleAuthorizationCodeFlow.Initializer object, and setting DataStore, ClientSecretsStream and Scopes properties on the object.

This is then being passed to a GoogleAuthorizationCodeFlow constructor as an argument.

Dan
  • 10,282
  • 2
  • 37
  • 64
0

You have a class Sample.

public class Sample()
{
    public string id { get; set; }
    public int key { get; set; }
}

This can be initialized as

var sample = new Sample {id = 1, key = "one"};

Then pass this sample as argument.

In your example they have done same thing to the parameter. This is called as class initializer.

Hope it helps.

Sandy
  • 11,332
  • 27
  • 76
  • 122
  • Is this the same thing as a class constructor where you can have parameters in the constructor? – Johann Jan 08 '14 at 11:01
  • In your example `GoogleAuthorizationCodeFlow` constructor needs an instance of `GoogleAuthorizationCodeFlow.Initializer` object/class. As I see `GoogleAuthorizationCodeFlow.Initializer` has a parameter less constructor. You code snippet is creating the class instance using class initializer and passing this as a parameter. Hope it was clear now. – Sandy Jan 08 '14 at 11:59