Tim Mitchell
Follow Tim Mitchell on Twitter  Like Tim Mitchell on Facebook  Subscribe on YouTube  Connect on LinkedIn  Subscribe to the Data Geek Newsletter

C# is Great, But…

When I get excited about something, I tend to be fairly tolerant of minor shortcomings. The same concept applies to some of my favorite technologies. The relatively new C# programming language is a perfect example. I have been using C# for a little over a year now, and I am very happy with it. The .NET Framework offers some exciting prospects, and the familiar syntax of C# is far easier to use than the mind-numbingly dull VB.NET. In fact, I have migrated much of my existing code to C#, and I use it in all but one of my new development projects.

That being said, I do have a few minor gripes about C#. The first is the inability to use a default value in a function declaration. For example:

public void MyFunction(string a = “”Hello””)
// Some action here

This code fragment, perfectly legal in C++ and Java, will allow MyFunction to be called with or without the string argument. In C#, the above listed function throws a compiler error CS0241, indicating that the use of default values are not permitted.

I’ve posted elsewhere on this topic before, and have received some good responses as to why this may be done. The crew at Microsoft elected to disallow this feature to ensure portability and flexibility of the applications and utilities written in C#; the defense given indicates that it is not reasonable to deploy code with static default values which may have to be changed in the future. Microsoft’s suggestion is to use overloaded functions rather than default values.

I completely understand the logic. If a development firm deploys an application, the values specified cannot be dynamically modified, thus building the business logic into the application code. However, there are myriad situations where this should be allowed. Much of my development work is done for internal use, and the codebase is maintained locally and can be easily recompiled if needed. Further, as a conscientious developer, I work to encapsulate business logic into elements outside the application code, usually in XML files or in a database table. For my purposes, this small omission is a more of a nuisance than a protective measure.

Nevertheless, I manage. For functions that I need to use default parameters, I write one or more overload functions that call the fully attributed function, such as:

public void MyFunction()

public void MyFunction(string val1)
// Application logic here

This is simple enough when referring to only one possible parameter, but the number of function overloads grows exponentially with the addition of each argument.

Again, this issue is a minor one in the overall scheme of things, but it is my opinion that Microsoft should err on the side of flexibility and allow its developer base to decide whether to use this functionality.

My second grievance comes in reference to the switch…case statement. Consider the following code block:

public void MyFunction(string a)
switch (a)
case “”red””:
// some action here

case “”blue””:
// some other action here

// some default code here

This code in C++ or Java will test the variable a; if it is “”red””, program control jumps to the case “”red””: line. Since there is no break statement after the case “”red”” action, program flow continues into the case “”blue”” code, then into default. Obviously, this “”fall through”” behavior can be very useful. However, this functionality too has been left out of C#; the above code throws a compile error.

Like my previous grievance, I have searched for a reason why this is done; however, I have yet to find an effective or authoritative answer to why this behavior is not allowed. Let me know if you find anything.

This post is not meant to be a gripe session about the horrific C# language; to the contrary, I am enamored with this new tool. Rather, I just had to get these minor complaints off my chest, in the hope that someone somewhere is listening and can do something to make a change.

About the Author

Tim Mitchell
Tim Mitchell is a business intelligence and SSIS consultant who specializes in getting rid of data pain points. Need help with data warehousing, ETL, reporting, or SSIS training? Contact Tim here: TimMitchell.net/contact

Be the first to comment on "C# is Great, But…"

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: