Freigeben über


New Features in C# 4.0 Paper, plus Feedback and Samples

Mads Torgersen, a C# PM and member of the C# design team, has worked with other members of the C# team to create a document that outlines the new features in C# 4.0. Along with Anders’ PDC talk, this is probably the best way to get an overview of what the C# team has planned for the next release of the language. I also want to make sure you know about the C# 4.0 samples. These short projects are designed to demonstrate how to use the new language features in C# 4.0. Finally, I provide a link to the Visual Studio 2010 CTP forums based feedback area, where you can comment on the new features Microsoft has planned for the C# language and the VS IDE. These forums provide a place where you can provide the PM’s who are driving the new features in VS 2010 with your valued feedback.

Here are the links to the:

Here are direct links to

Except for the CTP Feedback Forums, all the items listed above are found on the Code Gallery based C# Future site. That site provides a single nexus of information where you can learn about our plans for the next version of the C# language.

kick it on DotNetKicks.com

Comments

  • Anonymous
    November 04, 2008
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous
    November 04, 2008
    I know im going to start a huge debate here, but do we really need optional parameters in C#, one of the things i love the most about C# is how easy it is to read over the likes of VB and i refer specifically to optional parameters. I have always felt that optional parameters are ugly in written code and make it harder to read code and understand its intention, i have never liked the likes of M "Hello World",,,"Hello" For someone picking up the code, they would have to ask what are the three commas for? what parameters do they represent? I think (IMHO) that overloading methods is far better than optional parameters and makes the code far more readable. If im the only one who thinks that then ill eat my words. Thanks for listening You've been a great audience...

  • Anonymous
    November 04, 2008
    What is the difference between use of "dynamic" and "var" in C# 4.0

  • Anonymous
    November 04, 2008
    The comment has been removed

  • Anonymous
    November 04, 2008
    The comment has been removed

  • Anonymous
    November 05, 2008
    I agree with the first post. If you want a Visual Basic like language, then use Visual Basic. C# should retain it's purist ideals. Optional parameters look horid, and as far as I can make out have no use except for when programming with COM interfaces, and we have a workaround for that, System.Reflection.Missing. If the future of C# is becoming more like VB,  then I am dissapointed. VB is a mess of ByVal ByRef optonal this, end if, end this, end that that. Ouhgh!!

  • Anonymous
    November 05, 2008
    Ajit, The simple explanation is that var is strongly typed and resolved at compile time, while dynamic is not strongly typed and it is resolved at run time. But that perhaps begs the question. I'm putting together a post that will explain this in more depth. For now, a great way to get started understanding Dynamic is to watch Anders' talk from PDC: http://channel9.msdn.com/pdc2008/TL16/Default.aspx?wa=wsignin1.0

  • Charlie
  • Anonymous
    November 05, 2008
    I didn't finish to completely learn the 3.5 version, and we speak about 4.0 version. I saw the "Mads’ New Features in C# 4.0 Paper", I already finished reading it and looks interesting. I like dynamics and also, I the COM improvements are welcome for me.

  • Anonymous
    November 06, 2008
    The comment has been removed

  • Anonymous
    November 06, 2008
    We need better type inference in C#. Currently type inferrence in C# is really poor compared to F# or Haskell!

  • Anonymous
    November 06, 2008
    Im concerned how intelliesense will work with dynamics.. I mean, since the compiler at design time does not know which type your object is, how will intellisense react to the potential garbage method you write? Maybe they'll show the appropriate method available if the name is unique to the imported libraries? And yes, Im as concerned as everyone else with the optional parameters.. if one wants VB, one should use VB.

  • Anonymous
    November 07, 2008
    One other quick point I should make here. This syntax for optional parameters is not valid in C# 4.0: M "Hello World",,,"Hello" You can only use named parameters, you can't use this comma based syntax. The reason for this decision was to keep the syntax as clean and simple as possible. So the call above might look something like this: M(Title: "Hello World", Caption: "Hello"); Which I think is fairly clear as compared to the empty commas. Here we name the parameters we want to specify, one being Title, and the other being Caption. Then we put in a colon, and finally we list the parameter we want to pass in, which in this case is "Hello World" and "Hello." Again, I'll write more on this in a separate post.

  • Charlie
  • Anonymous
    November 07, 2008
    As a former Lisper, I absolutly have always wanted &optional, &key parameters with defaults.  BTW, I also want real macros...

  • Anonymous
    November 08, 2008
    I dare say C# 4.0's support for dynamically typed objects is leading to VC#A. Cheers! Amitabh

  • Anonymous
    November 08, 2008
    jQuery/ASP.NET/ASP.NET AJAX/ASP.NET MVC Visual Studio patched for better jQuery IntelliSense . Yes! Steven

  • Anonymous
    November 08, 2008
    I'm concerned by the fact that they try do everything (and more) in C#. It seems that it will be very difficult to understand at the end. I'm OK for the .NET framework being able to cope with dynamic languages, but why adding a dynamic keyword to C# ? C# was not designed to be a dynamic language at the beginning, hence the need to add this keyword maybe ? true dynamic languages do not have this keyword, and they don't need it !!! People are always wining about Java beginning to be complex (with all these libraries in the core JDK), but I'm curious to know what they already think about C# 3.0, and what they will think of C# 4.0. Maybe all of this is a matter of marketing after all ;-)

  • Anonymous
    November 09, 2008
    The C# langauage changes too faster to we learn it hardly.

  • Anonymous
    November 09, 2008
    Regarding the first post: we (at DlhSoft) have developed multiple products in C# basically because we have more C# experienced programmers. However, in some cases, we ended up with as many as 20-30 overloaded constructors (or methods) simply because in C# 3.0 we didn't have optional parameters. I think it's a good thing to have this in C# too, as a gain from "co-existence" of C# and VB .NET. It's not like "we will use VB .NET just because it has optional parameters", especially when we have a lot of experienced developers using C# already. It wouldn't be productive at all. And therefore I agree that purism/idealism should be sometimes defeated by practical reasons. Sorry guys...

  • Anonymous
    November 09, 2008
    The comment has been removed

  • Anonymous
    November 10, 2008
    Sorry but I have to agree with the first commenter and others - just say "no" to optional parameters in C#.  We do not need a language that "holds our hand" so much.  There is already a .Net language that does that, it's called Visual Basic.  I've worked with both languages quite a bit and I very much prefer the conciseness and, as another put it, purity of C# over VB's verboseness and hard to read ugly code.  I love you guys at MS, I really do, but I think you are missing the mark if you put optional parameters into C#.

  • Anonymous
    November 10, 2008
    Some seem to forget that C++ has optional parameters.  I know of many of the developers that I hwave worked with that are looking forward to them being added to have the feature more like C++.

  • Anonymous
    November 10, 2008
    ok , now i know there is someone else who feels like c# 4.0 is 'com and/or VB' oriented c#. yes,, dynamics is good way to avoid reflection mess, and code may look elegant. and variance seems handy. but i think optional parameters and named parameters are subjects to argue. does optional parameters feature seem to an effort to stand close to former VB developers. quote from Herve "Maybe all of this is a matter of marketing after all ;-) " i believe optional parameters and named parameters will end up with less readable code on our projects and everywhere on internet (may be it also belongs to us as the people who aware of the situation).

  • Anonymous
    November 10, 2008
    The comment has been removed

  • Anonymous
    November 11, 2008
    silashansen: Yes, that's ugly, which is why C#4 doesn't support it. Read up. If you skip parameters, you have to pass them by name, which preserves clarity. The reason default and optional parameters are both useful is because if you have a bunch of methods that can take more or less parameters, you will soon be drowning in overloads since it may be practical to have different combinations of the parameters use their default value.

  • Anonymous
    November 11, 2008
    As a development manager, I applaud the decision to include optional parameters and named arguments. I have never understood why the architects of C# felt it was better to be forced to write, test and maintain multiple versions of the same rountine when a single version woud do the job. I recognize overloading is useful in may circumstances, but there are also cases where it makes the developed code more difficult to understand, test and maintain.

  • Anonymous
    November 12, 2008
    There are a few new features coming out in C# 4.0. I gathered some posts that will help you to "get

  • Anonymous
    November 12, 2008
    I have to say that all of you should get over the optional parameter angst in C# 4.0 and quit making such a hullabaloo over it.  I've programmed with Visual Basic for many years (and still do) and C# for the last 4 years and I welcome the change. Let's face it:  if you don't like optional parameters and named parametes, JUST DON'T USE THEM!  If you're a manager and really want to stick it to your underlings, forbid its usage!  It's as simple as that.  I don't understand all the hoopla surrounding this simple feature. I prefer method overloading where the methods really do very simple tasks, but I've had to write numerous overloads for a method where the only call inside each overload is to a core method.  I really would have rather written a single core method where it made sense and provide optional values. All of C#'s changes and VB's changes are to bring them to essentially the same technological level and this is a most welcome change; I work in a shop where both languages are used extensively and it's really hard for my fellow VB programmers to use the technology that I use and conversely I the same when programming C#. Just get over it.

  • Anonymous
    November 12, 2008
    Charlie, what i would like is something like property pointers. AFAIK that's not possible now.

  • Anonymous
    November 13, 2008
    Most of the features in C# 4.0 are already there in VB. Sounds there are really few things to improve for those languages.

  • Anonymous
    November 14, 2008
    There is a very simple example to demonstrate why optional & keyword arguments improve code readability. Compare:  new MemoryStream(data, 0, buffer.Length, true) and  new MemoryStream(data, index: 0, count: buffer.length, writable: true)

  • Anonymous
    November 14, 2008
    Am I the only one here to recognize that the most significant change is the inclusion of covariance of generic type parameters! It will make generics a lot more useful tool and finally allow us to make them part of our APIs as they are polymorphic now

  • Anonymous
    November 14, 2008
    Personally, I am somewhat disappointed with this new set of features. There's nothing here that one would go "woo-hoo!" about, compared to C# 3.0.

  • Anonymous
    November 15, 2008
    I am new to programming. I have started working with c# 3.0 with .Net Framework 3.5. I am discouraged by the fact that a new version of teh language and new version of .net framework is coming out soon. Shoud I wait until teh new langugae debuts and then start my learning path? Or should I Continue with my learning with current standards. Please advise.

  • Anonymous
    November 15, 2008
    Peter, you should understand that new versions will keep coming out probably with the same rate (.NET 1.0 was in 2002, 1.1 in 2003, 2.0 in 2005, 3.0 in 2006, 3.5 in 2007, 3.5 SP1 in 2008). If you will wait for the next version to start learning, you will never finish :) In this industry, you have to be able to pick up the new stuff as you go. But, anyway, the majority of the .NET Framework remains, and of course everything that's in C# 3.0 will still be in C# 4.0, and heavily used as well... so you'd have to learn it one way or another - and then, why not start now?

  • Anonymous
    November 17, 2008
    Any chance .NET 4.0 will have the ability to turn background compilation on/off for the C# users? It would be nice to have the option at times to utilize it. That's about the only thing I miss from VB even though it is not optional there and would at times drag my system with VS 2003 but it didn't seem too bad in VS 2005.

  • Anonymous
    November 17, 2008
    The comment has been removed

  • Anonymous
    November 18, 2008
    break(n)-like feature would be greatly appreciated too!

  • Anonymous
    November 19, 2008
    Guys, there's nothing wrong with goto to break out of a loop. It's not an Inherently Evil Statement. It's a wrong tool for many jobs, but breaking out of nested loops isn't one. If you want labelled break just so that you won't have to write "goto", then you should pause for a moment and consider the reasons for your phobia :)

  • Anonymous
    November 20, 2008
    A while ago we announced Visual Studio 2010 and C# 4.0. In case you'd like to catch up and read articles

  • Anonymous
    November 24, 2008
    The new features will be very useful to anyone who wants to use C# in combination with the HTML DOM, Javascript, or COM.  Those purists who are not interested don't need to use them. However, there is one significant part of the story that has been skipped over in the presentations that I have seen.  That is the issue of passing delegates to dynamic code to be used as callbacks or event handlers.  Virtual Earth seems to be a favorite example.  But a C# to VE interface will be pretty limited unless you can call VE with C# methods that will be used by VE to pass back results or to handle VE events.  I assume this point has not been overlooked by the C# team.  So I am a little puzzled why nobody seems to be commenting on it at all.

  • Anonymous
    November 25, 2008
    The comment has been removed

  • Anonymous
    November 26, 2008
    Steve, you are so right. I have run into this so many times I have had to change the methods name to get around it. Heck, if you don't like optional parameters don't use them.

  • Anonymous
    November 26, 2008
    The comment has been removed

  • Anonymous
    November 30, 2008
    Derek, I see your points to some degree, but at the same time I'm not sure I can completely agree.  If done right, optional parameters with the proper defaults can make code more readable and easier to code.  And with intellisense you, as the creator of the method, can impart more knowledge about how the parameters are used - which ones are critical to the function of the method and which ones can be defaulted out. To your point on having two different methods with the same name, but different first parameter names, I can only see chaos from the point of maintainability down the road.  Consider the following use: Instance.A(100) Instance.A(200) Which one of these is the userid? and which is the companyid?  Sure we could say: Instance.A(UserID=100) or Instance.A(CompanyID=200) But do I really want to do all that typing?  Also, in spite of all these new 4.0 features, I think the authors of .NET Framework Design Guidelines would have something to say about method overloading and parameter positions...Shifting the meaning of the a parameter position (regardless of new language features) just seems like a bad idea in general.

  • Anonymous
    December 01, 2008
    The problem space in and of itself is a tricky one to solve. It either involves what I was talking about or optional parameters.  Neither solution is elegant and both have their own related drawbacks.  I guess I am ok with the way C# evolves as long as I can continue to use C# in the typesafe, non-optional-parameter based world I and my developers have been accustomed to.  Honestly, the situation does not arise that often for us because most of the time the issue can be worked around relatively easily by changing the design.  What I don't want to see is dynamics used everywhere.  I've been there.  It's called VBScript and that's partly how SQL Injection attacks were born.  Developers not understanding the type of variable they were working with and thusly not knowing when to protect said variable from certain attack vectors like SQL injection...

  • Anonymous
    December 01, 2008
    The comment has been removed

  • Anonymous
    December 01, 2008
    hmmmm... i think if you need 20 constructor overloads on a class then optional parameters arent going to save you from a completely horrible lack of design or thought about what you are doing... sorry but that's just insane. Would you prefer one constructor with 20 * n optional params? Uggh! That said, i was always opposed to optional params (esp of the ,,, syntax variety), but the way it's been done in c# 4.0 isn't too bad. Personally i prefer overloading because of the neatness it provides when cascading the 'optionals' down - but we'll see. At the end of the day,  if you design your systems and classes properly in the first place, optionals should really pose few issues, even to purists.  As an example, think in terms of parameter objects, (wrapping params into one single object and passing that) - we can use class initialisers already which have similar name/value syntax to optionals, so optionals may actualy be quite helpful in terms of creating strong 'Message' Types for services and hard to break contracts.  It's all about design - if you don't think through the code you write then optional params (or lack thereof) really arent' going to make a difference.

  • Anonymous
    December 01, 2008
    The comment has been removed

  • Anonymous
    December 02, 2008
    Perhaps it's the code I develop but I would find as construct like -- if (name in {"Fred", "Tom", Mary"}) -- extremely useful. I realize there are several ways to do this using the current syntax but don't feel any are quite as compact or clear from a documentation perspective.

  • Anonymous
    December 02, 2008
    I’m just prepering for a presentation of new features in VS 2010 and .NET Framework 4.0, so let me give

  • Anonymous
    December 03, 2008
    How about the ability to alias class and method names? That would be very useful to most of us who cannot invent proper class and method names as fast as we need to deliver the code. Here is a contrived example of what I find myself doing. public class SomeClass {     public void DoSomething() { } } Several weeks later after the class has been compiled into an assembly and the assembly has been referenced by everything else I find myself inspired ti rename it properly. public class ByteCounter {    public void CountBytes() { }   } Rebuilding the universe leads to all kind of breakage because the SomeClass has disappeard. So why not correct bad naming like so: public class ByteCounter aka SomeClass {   public void CountBytes aka DoSomething() { } } The compiler could surely recognized the old names and issue some warning so that subesequent correction can be made at some later time.

  • Anonymous
    December 04, 2008
    Wytek, I dont mean to be rude, but after a few minutes, or even after a few days, you should already know what the proper name of the class your writing is. More often than not, you should know the class name before you even start the code. As for the optional parameters, as previously stated, if people don't need/want it, then they shouldn't use it. To me it just makes C# more flexible by leaving out the defaults. It isn't exactly the same as overloading either. {end rant} Cheers!

  • Anonymous
    December 06, 2008
    To me, named optional parameters would be incredibly useful. It is ludicrous needing to have 20 constructers, when all you are logically trying to achieve is perhaps 5, but requiring 4 permutations of each (just for the sake of having to manually code in your optional parameters each time). And not only does it make it less maintainable, it becomes a lot less friendly to use, and find the version you are after. I think the frightened belief of those against this feature, stems from the fear that only one constructor would be created and with say 10 optional arguments. Therefore, there is no logical guidance of which parameters should be exclusively, required in combination. This discipline, I think belongs to partitioning and modelling the constructors correctly. Also, I believe the code should reflect as strongly as possible, the subject matter of the object, and not mainly lines of a bunch of copied and pasted constructors.

  • Anonymous
    December 08, 2008
    I don't get the rational for introducing VB-like features into C# and vice versa. These are 2 different languages and attempts to make them similar will, at best, create two languages that are indistinguishable. And at worst, create 2 languages that appeal to neither VB followers nor C# followers.

  • Anonymous
    December 08, 2008
    For those in the "overloading is better than optional parameters" camp - why not just treat optional parameters as a syntactic sugar for hand-writing all those 20 overloads? In which case, how is it any better or worse?

  • Anonymous
    December 09, 2008
    The comment has been removed

  • Anonymous
    December 11, 2008
    The comment has been removed

  • Anonymous
    December 12, 2008
    The comment has been removed

  • Anonymous
    December 19, 2008
    If I wanted to waste my time ever while developing a project I would use VB.NET with its horrible options like Optional Parameters. Then again, i someone likes using them they can use VB.NET and be happy with it. But this is the B@st@rdization of C#. C# is finally making its mark in history and wil get spoilt like this. Why dont some of the greatest languages with the C syntax like C++ and Java dont have optional parameters? I'm sure others would be feeling the same...

  • Anonymous
    December 21, 2008
    The new features seems really nice, but it's kind of hard for me to tell as the docks are posted in the docx format of all things. We are on the web theese days, and you guys seem rather clever, so I do hope to see a html vertion of your future documents :-) There are just no excuses to deliver documents in such an mediocre format, as not all of us has jumped on the Office bandwagon. The only alternative for me is the openoffice tools, and they are ...less then satisfactory... I find.  But keep up the good work on the framework guys! (but please up the ante on the docs ;-)

  • Anonymous
    December 31, 2008
    For all of you ranting about optional parameters being new to C# v4.0...  Just remember that optional paramters have been in the language since v2.0 (although only used with custom attributes).  So this is not a new feature, just a new application of an existing feature.   I am not surprized about this change, nor the syntax.  It seems to be a logical improvement.  It also simplifies some problems with COM interoperability.  For some, this is a much needed improvement.  

  • Anonymous
    January 07, 2009
    I love the optional and named parameter ! The optional parameter recalled the days when I use C++ and the named parameter have been available for years in VB and finally in C# ! The use of dynamic type in the example of COM (e.g. automate Excel) is a great feature, but it seems to be a bit LATE (well, I should say RATHER LATE) since on one hand, Microsoft had released the Office 2007 XML document file type and encourage people to use them instead of using COM to automate Word/Excel, but on the other hand the dynamic type for COM is telling the developers that - hey folks, you can now automate Excel through COM easily in C# 4.0 by making use of the dynamic type, and forget about the Office XML document type ! It seems to be a contradiction !! But nevertheless, I hope that IntelliSense will be provided when using the dynamic type to automate Word/Excel App ! Last but not least, recalled the days that it was a total failure to Borland Delphi 4.0 and Borland C++ 4.0, I DO sincerely hope that the number 4.0 is NOT causing another failure to C# !

  • Anonymous
    March 17, 2009
    I think that optional parameters could be a good improvement, because I frecuaently face that problem of having 20 constructors and 20 methods that do the same thing, but nevertheless it is not my highest priority. On the other hand dynamic type would be confusing, source of bugs and as far my main  concerns is performance, I will stay away of that. Furthermore, most of dynamic type issues can be more elegantly and efficiently imlemented if C# allows struct inheritance, field interfaces and operator constrains.

  • Anonymous
    September 24, 2009
    Really miss Spec# non nullable reference types would remove ALL those if null checks for collections etc

  • Anonymous
    June 13, 2010
    The comment has been removed

  • Anonymous
    June 13, 2010
    The comment has been removed

  • Anonymous
    August 22, 2010
    bimbim.in/.../c-sharp-40-features.aspx

  • Anonymous
    June 10, 2014
    I find that I am interested in the part of barcode scanner: www.businessrefinery.com/.../vbnet_barcode_generator.html This is something on the contrary.

  • Anonymous
    December 21, 2014
    I'm confused with what is stating here but I just want to know if this product is same with what this site offers? www.pandapaperroll.com