Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

10-4 Episode 12: Simplifying Your Code With C# 4.0

Download

Right click “Save as…”

In this episode of 10-4, we take a look at some features coming with C# 4.0 that will help you simplify your existing code (as well as making new code you write more simple). There's already a plethora of C# 4.0 content you can find out there (including here on Channel 9 itself), so we will dive in specifically to how the combination of named and optional parameters will help you simplify your code.

While we aren't specifically using the CTP, most (if not all) of the code shown in this video should work in the CTP as well.

For more 10-4 episodes, be sure to visit:
http://channel9.msdn.com/shows/10-4

C# Dev Center:
http://msdn.microsoft.com/en-us/vcsharp/default.aspx

Community Convergence Blog:
http://blogs.msdn.com/charlie/default.aspx

Managed World Blog:
http://www.managed-world.com/

Visual Studio Topic Area on Channel 9:
http://channel9.msdn.com/VisualStudio

10-4! Over and out!

Tags:

Follow the Discussion

  • Edward MoemekaMoemeka Me
    Man,
      you guys are making my expensive college education in Computer Science even more useless.  Why did I need to learn about method overload again?  Quick question.  Does the default value of the default parameter you specify get exposed through intellisense to the comsumer of the method?
  • Vesuviusvesuvius Count Orlock
    Hello Jay,

    I cannot see the code when I play it in the Silverlight player, I thought the new VS designer had a vector based code editor? Hope the download is clear!
  • Vesuviusvesuvius Count Orlock
    Wait until you have to maintain code writte by somebody else, you will find that the concepts your learnt were well worth it. Its fine for code written in 2010, but there is a lot of code writtem before now, that even colllege graduates in 10 years time will have to maintain. 

    Cutting corners in Computer Science doesn't not really exist, unless you only ever work on code written from a specific date.
  • Allan LindqvistaL_ Kinect ftw
    one thing that bugs me is that the default values for a method are moved to the call site at compile time.. in other words, if you have a default value for an argument, that argument wont change in old code even if you change it in your method after the old code is compiled. old code will still use the old default value..

    this is just a detail though Smiley default values are still awsome. but its something to keep in mind Smiley
  • tsilbSlackmasterK This Space ​Intentional​ly Left Blank
    Wow, this video is so clear I thought I could click the Visual Studio 'Close' button.
  • claudy27claudy27 claudy27
    same here...lol
  • Weird, I'm not having the same issue on my end :/. However, I have forwarded another issue to the C9 dev team as the bottom portion of the video is cut-off when going fullscreen Sad.
  • Nah, like others are mentioned, you can still get a lot of value from that education Smiley. Glad you like the videos and product though!
  • You know, off the top of my head I'm not sure if the value is included in Intellisense or not. If it's not (which I'm thinking is the case), it sounds like a great feature suggestion Smiley. I'll check for sure tomorrow. Thanks!
  • I must say I've never been a fan of optional parameters. For the COM scenario i find it quite great, but for general class development i find it a bad practice.

    I don't hope people will start abusing this feature and kill the whole overloading design (which is more expressive if you ask me).
  • FlatlinerFlatliner With our thoughts we make the world.

    I'm glad named and optional parameters are added to C# 4.0. But can someone explain the syntax for named parameters. Why do they use a colon  : rather than an equals? e.g. GenerateChart(copyToWord = true);

  • Sven GrootSven Groot Don't worry... I'm a doctor.
    Because that's already valid syntax in C# 3.5. In your example, if there is a local variable called copyToWord, what that code does is set it to true and then pass the value true to the first parameter of the method. They needed something that wouldn't conflict with existing syntax and potentially change the meaning of old code.
  • PerfectPhasePerfectPhase "This is not war, this is pest control!" - Dalek to Cyberman

    Isn't this a good thing, it preservers the original intent of the code?
  • I suggest either the presenter or someone on C9 look at it before posting!

    That would increase the usability of the videos !!!

    If you are presenting something about code and camera is not focussed on code, what is the point of the video then?
  • stevo_stevo_ Human after all
    Technicallly the calling codes intent was 'whatever you think best'.. you shouldn't really pick an overload because the current behavior is what you wanted, you should be explicit unless what you actually meant 'I don't care'.. I don't think theres a problem with the chosen behavior however.
  • dev, what specific problem are you having? The code is plenty readable on my end. The problem I have (and it exists with all Channel 9 videos I checked out last night) is that the bottom 1/5-1/6 of the screen is cut off when going to full screen on the Silverlight player. Yes, if you try to view the video in place, you won't be able to see the code as it's too small. Try going full screen on the player though (or downloading the video). 
  • Matt, in my opinion, it really depends on the intention of the code. Most of the times I have seen method overloading used to express optional parameters, it is really trying to communicate the idea of "default values". And in my personal opinion, using method overloading to express default values is, in fact, an abuse of method overloading, not vice versa.

    Specifying default values via optional parameters is a more intuitive way to express the concept rather than a bunch of method overloads that simply call a different method overload while passing in a default value to an extra parameter. I think method overloading is ideal for a situation where the overloads are truly different in nature (like one method accepting a string parameter that is the path of a file, and another overload accepting a FileInfo of the file).
  • I get your point, and I agree with the usage you describe. I guess we'll just have to see how the community and the different API developers grasp this new feature.

    I'm just wondering who actually uses it. I currently work at a company that develops in VB.NET, and none of my fellow developers ever use the optional parameter ability. Where does this general dislike come from, if it's not such a bad idea?

    Perhaps if we see APIs in the future (from Microsoft in particular) where optional parameters are used in a good way. It might become a more accepted practice.

    I choose to remain skeptical however.
  • Hello there,

    Not that I don't appreciate the value of these videos.  But when are we going to hear about really really cool stuff going on in the next .net framework? For instance I remember that when 2.0 was in beta most of the noise you see around was that the framework doesn't inlined functions if their parameters are ValueTypes, that's serious performance!!

    And what about generics for arithmetic stuff? in 2.0 (or 3.0, 3.5 for that matter) you can write T Sum<T>(T a, T b) { return a + b; } which is great for writing true library templates ala old C++.

    Jason, Hope you can show us more about this
  • Definiteley a good question, however i thing that if intellisense were to provide the default value (which sounds correct) it should also provide information on what that default value means. In that sense im guessing that all that information has its own  place in the xml tags used for commenting  methods, properties , etc. Maybe a good idea would be to provide a new attribute for  the param tag used to comment method parameters (which i think will be an easy thing to ).


  • 1.) Optional Parameters are a really cool feature. The reason why VB.NET developers did not use it as often is because if you make a dll with your code and a C# developer used it then he had to type every parameter because he had no support for leaving it out.

    2.) It is not a question of design if you use method overloading or optional parameters. If you have a function with 10 arguments where you should be able to only specify those which you like then you would have hundreds of methods that only call other methods with default values. Often you choose only the most important combinations, but often you also have more then 20 methods.

    3.) Optional Parameters should not only accept constant values like true or false, but in most cases you need dynamic values. I have a function WriteProtocolEntry(Source as String, Message as String, Date as DateTime)
    Most Time Date is DateTime.Now. You can not default date to DateTime.Now so you have to use method overloading again.
    In many of my read/write Functions I have a Timeout. Defaulting it would be nice, but if you move the default value to a config file to change it very easy then you have to use method overloading again or default it to nothing and at the start of the method you write (in VB.NET):
    If Timeout Is Nothing Then
        Timeout=Config.Timeout
    EndIf

    If Timeout is an Integer even this is not possible and you have to default it to 0 or -1 or if it is a DateTime you could use DateTime.MinValue and hope that noone will ever write a Protocol Entry with 1/1/0000


    Another example of the advantage of optional parameters is the creation of Exceptions:

    Without the use of optional parameters you had to create 6 constructors:
    Public Sub New()
    Public Sub New(Message as String)
    Public Sub New(Message as String, InnerException as Exception)
    Public Sub New(AdditionalInfo as String)
    Public Sub New(Message as String, AdditionalInfo as String)
    Public Sub New(Message as String, InnerException as Exception, AdditionalInfo as String)

    Each Constructor only calls MyBase.New and I think MyBase.New() will do things like Me.New(Nothing,Nothing)
    With Optional Parameters you could use:

    Public Sub New(Message as String=Nothing, InnerExceptions as Exception=Nothing,AdditionalInfo as String=Nothing)
        MyBase.New(Message,InnerException)
        Me._AdditionalInfo=AdditionalInfo
    End Sub

    And if you want to compile it then you would notice that you have two constructors with the same parameter types (Message as String) and (AdditionalInfo as String).

    4.) For watching videos I use Media Player Classic and the KLite Codec Pack. With this player you can change the size of the video with the Num Pad.
  • kzukzu kzu
    Go watch the entire "The Future of C#" session by Heljsberg. Much more valuable.
  • Wow, did we miss the mark that much? For a more in-depth, complete video, yes Future of C# (or other C# videos) are much more suitable. However, for a 5-minute screencast that devs can watch in the morning/evening or during their commute to digest various features in VS2010? I think 10-4 is a better fit for that than an hour video interview. To each their own, I suppose.
  • Great Video!! Cleared things around the default value.

  • DebasishDebasish

    @vesuvius:Same here. the video is not clear n the code is not visible when going for full screen mode.

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.