InfoQ

News

C# Feature Focus: Optional and Named Parameters, COM Interoperability

Posted by Jonathan Allen on Oct 27, 2008

Community
.NET
Topics
Language Design ,
Interop
Tags
COM ,
C# 4 ,
C#

Due to overwhelming customer demand, Anders has "relented" and allowed Optional and Named Parameters in C#. Just like VB, optional parameters are supported by including a default value. And like in VB, required parameters must all appear before any optional parameter.

Named parameters are also supported fully. This allows developers to specify the parameters in any order they see fit. This works with normal as well as optional parameters, one would expect to only see it when dealing with the latter.

When resolving expressions used as arguments, the order is strictly determined by the code making the call irregardless of the order in the function's signature. And though not recommended, this means expressions with side effects can be more or less safely used as arguments.

COM libraries have a bad habit of returning values as type Object. To address this, C# 4 will automatically promote any object returned by COM to the "dynamic" type. As we mentioned in the previous article, this allows late-bound calls on the object just as if the developer had manually written all the necessary reflection code.

The "ref" modifier will no longer be needed for COM calls unless by reference semantics are actually needed.

In the end this means code that used to look like this:

//C# 3
var a = (IFoo) obj.Foo(ref missing, ref missing, 
                       ref missing, ref missing, 
                       ref missing, 5, ref missing, 
                       ref missing, ref missing);
var b = (IBar)a.Bar();
var c = b.Value;

//C# 4
dynamic obj = //some COM or DLR object
var c = obj.Foo(clientId := 5).Bar().Value

There was also a rumor about parameterized properties. We will post an update once we have more information on it.

Overwhelming demands, but from who.. by Francois Ward Posted Oct 28, 2008 8:54 AM
Re: Overwhelming demands, but from who.. by Richard Philipsen Posted Oct 29, 2008 7:47 AM
Re: Overwhelming demands, but from who.. by Francois Ward Posted Oct 29, 2008 10:37 AM
Re: Overwhelming demands, but from who.. by Jonathan Allen Posted Oct 29, 2008 3:58 PM
  1. Back to top

    Overwhelming demands, but from who..

    Oct 28, 2008 8:54 AM by Francois Ward

    I know there's huge demand for some of these features, but I'm wondering from which demographic its coming from. The .NET framework supports (or will support) most languages you'd ever need...

    Thus, I can't help but feel that these "demands" come from people who shouldn't even be using C# in the first place. I used to ask for optional/named parameters a lot, but I also have a VB6 background on top of having worked a lot in dynamic environment. C# was more the "purist" language. If C# goes the "kitchen sink" way... which language will be there for purists (and considering the rise in popularity of C#, and the ups and down of Java, it seems like there definately is a market for these "pure" languages).

    Right now all I can see, is C# with a bunch of custom FXCop rules to weed out potential messes.

  2. Back to top

    Re: Overwhelming demands, but from who..

    Oct 29, 2008 7:47 AM by Richard Philipsen

    I agree 100%.

    I come from an C & IDL background as two different languages for two different purposes. I now see C# growing out into a massive mess (the kitchen sink you're talking about) and that's not a good thing.

    Especially with the possibilities to easily interconnect to different .NET assemblies in different languages, I don't understand the need to stick everything in one basket instead of just writing different, smaller, more specialized languages for .NET.

    In this fashion, each team/company will only be allowing a subset of C# which will only confuse new developers/teammembers who were used to using the rest.

    I guess I'll just watch it and continue pondering.

  3. Back to top

    Re: Overwhelming demands, but from who..

    Oct 29, 2008 10:37 AM by Francois Ward

    Not a big need to watch to see what will happen: it happened before, so the outcome is obvious.

    Java anyone? It used to be the lean and mean "purist" language. It had very few, very consistant constructs. It -was- missing some (generics), and they were added... but then they felt the need to add everything. What happened? Exactly what you mentionned: people turned to arguably better, specialized languages (Groovy, Scala, etc). Java isn't going away because it IS still good, and it is the cobol of our time (so much done in it), but people aren't happy about it. Thats half the reason .NET was successful in the first place.

    No one want a kitchen sink language... C++ was enough. Same thing will happen with C#... someone will build a new language, similar to C#'s root (plus the actually useful extensions, like lambdas and LINQ), and C# will be legacy "so-much-code-already-written-we-cant-throw-it-away" stuff.

    Does no one learn from history?

  4. Back to top

    Re: Overwhelming demands, but from who..

    Oct 29, 2008 3:58 PM by Jonathan Allen

    The target demographic is people who need to use COM.

Educational Content

Brian Marick on 4 Challenges and 5 Guiding Values of Agile Software Development

Brian Marick takes us through a quick tour of the most important values and challenges to adopting Agile successfully (they aren't the typical challenges and values we hear in the community).

Are You a Software Architect?

The line between development and architecture is tricky. Does it exist at all? Is an ivory tower actually needed? There's a balance in the middle, but how do you move from developer to architect?

Agile – A Way of Life and Pragmatic Use of Authority

The word 'authority' sometimes produces an allergic response in hard-line agilists. Freedom and authority – both are bad if misused and both are good if used in right spirit for a noble cause.

Getting Started with Grails, Second Edition

"Getting Started with Grails" brings you up to speed on this modern web framework. Companies as varied as LinkedIn, Wired, and Taco Bell are all using Grails. Are you ready to get started as well?

Using ITIL V3 as a Foundation for SOA Governance

Those familiar with only ITIL V2 often scoff at the thought that ITIL could serve as a governance framework for SOA. With ITIL V3, the focus of the framework shifted towards service-orientation.

Adrian Colyer on AspectJ, tc Server and dm Server

SpringSource CTO Adrian Colyer discusses AspectJ, SpringSource's dm Server and tc Server products, OSGi and Scrum.

Adam Wiggins on Heroku

Heroku's Adam Wiggins talks about Rails, Background Jobs, Add-Ons, Ruby, and how Heroku manages to work around Ruby's inefficiencies using Erlang and other languages.

SOA as an Architectural Pattern: Best Practices in Software Architecture

For Grady Booch the foundation of a good architecture is patterns, SOA being just one of many patterns. In this Second Life presentation, Booch attempts to bring more clarity on what architecture is.