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

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

The Discussion

  • User profile image
      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?
  • User profile image
    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!
  • User profile image
    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.
  • User profile image
    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
  • User profile image
    Wow, this video is so clear I thought I could click the Visual Studio 'Close' button.
  • User profile image
    same here...lol
  • User profile image
    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.
  • User profile image
    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!
  • User profile image
    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!
  • User profile image
    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).
  • User profile image

    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);

  • User profile image
    Sven Groot
    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.
  • User profile image

    Isn't this a good thing, it preservers the original intent of the code?
  • User profile image
    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?
  • User profile image
    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.
  • User profile image
    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). 
  • User profile image
    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).
  • User profile image
    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.
  • User profile image
    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
  • User profile image
    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 ).

  • User profile image
    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

    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)
    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.
  • User profile image
    Go watch the entire "The Future of C#" session by Heljsberg. Much more valuable.
  • User profile image
    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.
  • User profile image

    Great Video!! Cleared things around the default value.

  • User profile image

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

Add Your 2 Cents