Thursday, February 25, 2010

C# 3.0 Implicit Type Local Var

If you have been reading the other blogs, you probably pause when you read the title of this blog. C#?! Why would an Open Source blog write and discuss anything to do with the at one time the biggest "opponent" of Open Source after all. These days, the official press release and the quite hefty check that Microsoft gave Sun, indicate that Microsoft has, whether this is true or not, come to term with the fact that Open Source is here to stay. In fact, Microsoft decides to become the primary sponsor of one of the plethora of Open Source conferences. Sadly, I forget which one though.

For many, especially the clients and organizations in this 21st century, this is indeed a welcoming sign. While each software vendor always tries to stake as much in every contract and purchase, the reality for many including my case in the recent project, we have to deal with technologies that come from both camps anyway. On site, I have to use the Microsoft SharePoint (MOSS 2003) for example as the key for the Change Management process. Yes, with $53-Million at stakes, we need a better way to collaborate amongst ourselves and with key stakeholders beyond email exchanges.

We choose the Oracle/PeopleSoft to manage the Student Life Cycle with Oracle 10g backend, BEA WebLogic App Server and Apache Web Server. As the Security Architect, I am gladly using the Java-binding and through the PeopleSoft Component Interface, I do have the options of creating bulk User Profiles/Accounts.

But, for the SharePoint, User Profiles and other information are tightly integrated to our LDAP infrastructure. And when it was time for me to "bulk" create the User Profiles, I welcome the option of using the C# and the XML Web Services. I remembered that I did not have a lot of time, only 1 day I believed, as I was about to fly overseas and there were requests to add hundreds more of new accounts.

After digging through some articles on the MSDN website, I came up with a nifty way to use C# and WSDL, to "talk" to the SharePoint "User Profile" Web services and create each Profile along with the appropriate Role in a matter of minutes. By 5:00 PM that day, I was done with that work, and more than ever ready for the flight to see my family and relatives 12000 miles away in Bali.

The bottom line is that very often in our life, we do not have the luxury to pick sides. I am a firm believer in Open Source. But as the story illustrated above could tell, in many cases, I have to deal with the other ecosystems as well, Oracle, PeopleSoft or Microsoft. Failures to do so would have unpleasant consequences especially comes the performance review.

After working with Java since 1999, I find the transition to C#, well, rather pleasant. The learning curve is not as steep as I remembered making the Perl journey in 2002.

For today, I would like to go over one of the "new" feature that is available in C# 3.0, which is the Implicit Type Local Var.

Here is one code example with the Bali flair illustrating the Implicit Type Local Var:
using System;
using System.Collections.Generic;

namespace Balisoft.App.CSharp30
{
class Program {
static void Main(string[] args)
{
List subdistrics = new List();
subdistrics.Add("Baturti");
subdistrics.Add("Penebel");
subdistrics.Add("Pupuan");
subdistrics.Add("Marga");
subdistrics.Add("Selemadeg");
subdistrics.Add("Kerambitan");
subdistrics.Add("Tabanan");
subdistrics.Add("Selemadeg Barat");
subdistrics.Add("Selemadeg Timur");

foreach (var a in subdistrics) {
Console.WriteLine(a);
}
}
}
}

Here is the nice formatted version of the program file:



Notice in particular the following code segment:

foreach (var a in subdistrics) {
Console.WriteLine(a);
}


That is it for now....I would discuss the other features of the C# 3.0 in the future.






No comments:

Post a Comment

Followers