Curly Monotony

Dispite C# being a mainstream programming language that is not as powerful as some of the research programming languages out there, it is increasingly comming together with nice features and syntax that work well together.

So first 7 examples of syntax monotony in C# –

1. Methods [1.0]

public class Person
{
     public static Person Create(string name, …)
     {
          return new Person(name, …);
     }
     …
}

2. Constructor Methods [1.0]

public class Person
{
     public Person(string name, …)
     {
          this.Name = name;
          …
     }
     …
}

3. Properties [1.0]

public class Person
{
     public string Name
     {
          get { return _name; }
          set { _name = value; }
     }
     …
}

4. Automatic Properties [3.0]

public class Person
{
     public string Name { get; set; }
     …
}

5. Object Initializers [3.0]

var p = new Person()
{
     Name = “John Doe”,
     …

6. Anonymous Classes [4.0]

var p = new { Name = “John Doe”, … };

7. Named & Default Method Parameters [4.0]

public Person(string name, int age, Permission pms = Permission.Default, …)
{
     …
}

var p = new Person(“John Doe”, 34, Permission.Default);
var q = new Person(“John Doe”, 34);
var r = new Person(age = 34, name = “John Doe”);
var s = new Person(age = 34, pms = Permission.Default, name = “John Doe”); 

So what we see here, is that expression of value construction, be it via method calls, constructor method calls or property access now has an strange familiarity. Everything is “curley” and very nicely aligned.

Note also, that value construction can be recursive (objects) and this is now possible to do in a quite beautiful way via either recursive object initializers (using properties instead of some fixed pre-defined sets of combinations) or via nested named parameters which also now look a bit like object initializers.

One might perhaps argue: why do we have both, but of course there are reasons to keep the possibility to deterministically initialize properties and interleave these with side-effecting computations.

Advertisements

About xosfaere

Software Developer
This entry was posted in Uncategorized and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s