10-4 Episode 12: Simplifying Your Code With C# 4.0
- Posted: Mar 11, 2009 at 10:04 AM
- 80,082 Views
- 25 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
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!
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.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
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?
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!
Cutting corners in Computer Science doesn't not really exist, unless you only ever work on code written from a specific date.
this is just a detail though
I don't hope people will start abusing this feature and kill the whole overloading design (which is more expressive if you ask me).
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);
Isn't this a good thing, it preservers the original intent of the code?
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?
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'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.
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
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.
Great Video!! Cleared things around the default value.
@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