These days I have been bitten by the C# bug. As most would agree, the effects are "addictive". Migrating from one to Language to another has always been like shifting homes, which you wouldn't do unless forced to. But the .NET/C# team is ensuring that this "migrating" is something that everyone would want to do.

In my latest quest for writing a "Files and Folder Sync" application, I discovered some cool new features, that swiped away the earth below my feet. My application has a simple class called CFolderSync, with 6 members. I want to have Read-Write properties for these members. So this is how I would do that in traditional way

<Code Snippet - CFolderSync in Traditional way>

public class cFolderSync
        {
            private bool bFolderLocation1Validated;
            private bool bFolderLocation2Validated;

            private string sPrimaryLocation;
            private string sSecondaryLocation;

            private DirectoryInfo dirInfoLocn1;
            private DirectoryInfo dirInfoLocn2;

            public bool bFolderLocation1Validated 
                    {
                        get{return bFolderLocation1Validated;}
                        set{bFolderLocation1Validated=value;}
                    }

           public bool bFolderLocation2Validated 
                  {
                      get{return bFolderLocation2Validated;}
                      set{bFolderLocation2Validated=value;}
                  }

          public string sPrimaryLocation
                 {
                     get{return sPrimaryLocation;}
                     set{sPrimaryLocation=value;}
                 }

          public string sSecondaryLocation
                {
                    get{return sSecondaryLocation;}
                    set{sSecondaryLocation=value;}
                }

         public DirectoryInfo dirInfoLocn1
               {
                   get{return dirInfoLocn1;}
                   set{dirInfoLocn1=value;}
               }

         public DirectoryInfo dirInfoLocn2 
              {
                  get{return dirInfoLocn2;}
                  set{dirInfoLocn2=value;}
              }

</Code Snippet>

As you can see here, I am simply trying to "establish" properties for the Private fields. Nothing fancy here. In fact most often than not that's all we want and we have in the Get and Set. In C# 3.0+ we can achieve the same in much more cleanly manner

<Code Snippet - cFolderSync the C# 3.0 way>

public class cFolderSync
        {
            public bool bFolderLocation1Validated { get; set; }
            public bool bFolderLocation2Validated { get; set; }

            public string sPrimaryLocation { get; set; }
            public string sSecondaryLocation { get; set; }

            public DirectoryInfo dirInfoLocn1 { get; set; }
            public DirectoryInfo dirInfoLocn2 { get; set; }

            //Using the Auto Getter and Setter enhancement in .NET 3.5
        }

</Code Snippet>

So we see even though we imply "public", but the moment we get this empty get and set, Compiler (.NET engine some would say) makes the necessary arrangements to make the member as private and the get and set property as public. That's why we call this as Auto Getter and Auto Setter.

This is only one side of the coin. Given the above Class, how do we plan to initialize an instance of this object...

<Code Snippet - Object Initialization the C# 3.0 way>

cFolderSync oFolderSync = new cFolderSync
        {
            bFolderLocation1Validated = false,
            bFolderLocation2Validated = false,
            sPrimaryLocation = string.Empty,
            sSecondaryLocation = string.Empty,
            dirInfoLocn1 = null,
            dirInfoLocn2 = null
        };

</Code Snippet>

The most interesting part is how the Intellisense works...

image

...

...

image

...

...

image

We can see how intelligent our intellisense is. It only displays the members which are yet to be analyzed. The only problem I faced with the Auto Getter and Setter  was that, if I want to have a Read-Only or Write-Only property, we simply cannot do a {get;} or {set;} respectively. But that doesn't mean we cannot have Read-Only or Write-only property. We can do the following..

{get; private set;}

Then we use constructors to initialize the objects. More reading recommended

Auto Implemented Properties: http://msdn.microsoft.com/en-us/library/bb384054.aspx

Implementing Immutable Class having Auto Implemented Properties:

http://msdn.microsoft.com/en-us/library/bb383979.aspx

I hope you enjoyed this blog. Enjoy the .NET 3.5ish way;