15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

inTroduCTion<br />

optional <strong>and</strong> named Parameters<br />

Optional parameters <strong>and</strong> named parameters have been in Visual Basic for some time but have not been<br />

available to <strong>C#</strong> until the .<strong>NET</strong> 4 release. Optional parameters allow you to provide default values for some<br />

of the parameters of your methods <strong>and</strong> allow for a type of overloading by the consumer, even if there is only<br />

a single method in place to deal with all the variants. Here’s an example:<br />

public void CreateUser(string firstname, string lastname,<br />

bool isAdmin, bool isTrialUser)<br />

{<br />

}<br />

If you wanted to overload this <strong>and</strong> have default values for the two bool objects, then you could easily have a<br />

few more methods that populate these values for the consumer <strong>and</strong> then make a call to the master method to<br />

actually create the user. Now with optional parameters, you are able to do something like this:<br />

public void CreateUser(string firstname, string lastname,<br />

bool isAdmin = false, bool isTrialUser = true)<br />

{<br />

}<br />

Looking over this bit of code, the parameters firstname <strong>and</strong> lastname do not have a default value set,<br />

while isAdmin <strong>and</strong> isTrailUser do have default values set. As a consumer of something like this, you are<br />

now able to do some of the following:<br />

myClass.CreateUser("Bill", "Evjen");<br />

myClass.CreateUser("Bill", "Evjen", true);<br />

myClass.CreateUser("Bill", "Evjen”, true, false);<br />

myClass.CreateUser("Bill", "Evjen", isTrailUser: false);<br />

The last example makes use of named parameters, which are also a new feature for <strong>C#</strong> in this release<br />

of the .<strong>NET</strong> Framework. Named parameters will potentially change the way you write your code. This new<br />

feature will allow you to make your code easier to read <strong>and</strong> underst<strong>and</strong>. As an example of this in action,<br />

take a look at the File.Copy() method of the System.IO namespace. Typically, it would be constructed<br />

similarly to this:<br />

File.Copy(@"C:\myTestFile.txt", @"C:\myOtherFile.txt", true);<br />

In this case, this simple method is working with three parameters, but what are the actual items being<br />

passed into the Copy() method Unless you know this method backward <strong>and</strong> forward, it is hard to tell what<br />

is going on by just glancing at this method. Using named parameters, you are able to use the parameter<br />

name in the code prior to the value being provided, as in the following example:<br />

File.Copy(sourceFileName: @"C:\myTestFile.txt",<br />

destFileName: @"C:\myOtherFile.txt", overwrite: true);<br />

Now with the named parameters in place, you can more easily read <strong>and</strong> underst<strong>and</strong> what is going on with<br />

this line of code. Using named parameters makes no difference to the resulting compilation; they are only<br />

used in the coding of the application.<br />

Covariance <strong>and</strong> Contravariance<br />

Covariance <strong>and</strong> contravariance were included in prior versions of the .<strong>NET</strong> Framework, but they have been<br />

extended in .<strong>NET</strong> 4 to perform even better when working with generics, delegates, <strong>and</strong> more. In the prior<br />

versions of .<strong>NET</strong>, you were able to use contravariance with objects <strong>and</strong> arrays, but, for instance, you were<br />

unable to use contravariance with generic interfaces. In .<strong>NET</strong> 4, you are able to do this.<br />

liV<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!