main banner

Development

Methods in .NET - Overview and default values (Part 1)

The .NET framework offers a lot of functionality in the definition and handling of methods.  Besides the typical behavior of methods/functions in any programming language, .NET provides extra features when creating and using functions. This article is intended to describe and explain some of the possibilities around method signatures.


  1. Overview and optional arguments: summary of the article and explanation of named and optional - default values - in method’s arguments.
  2. Method parameter: how to handle a variable number of arguments.
  3. Extension methods: extending or adding methods for existing types.
  4. Generic constraints on type parameters: exposition in the usage of constraints to generic parameters in methods.

Named and Optional arguments

C# 4.0 added the feature of named and optional arguments; method overloading is no longer necessary to handle default values for arguments when calling methods. Now, through the method’s signature, we can indicate to the framework the aim to have a value - in any given parameter - when omitted.

In the following method, which “draws” a circle, we defined that the radius will have a preset value of 1. 

Methods in .NET Part 1

If we make the following calls to the method we will get the Output shown.

Methods in .NET Part 1

Methods in .NET Part 1

Notice that the second call only contains two arguments, so the framework sets the radius variable to 1, as we can validate in the output messages.

It’s important to mention that in the signature all required parameters must precede the optional ones.

The named arguments, is another feature which is, to a certain degree, related to optional arguments. In our previous example, let’s change the method’s signature to have a default value set to 0 for x and y arguments as follows.

Methods in .NET Part 1

With this change, we may be able to call the method without arguments and the result would be a circle centered at (0, 0) with a radius of 1. Besides this possibility, we can call the method with any argument, and we can omit the ones we would like to use with default values. For instance if we would like to have a circle with center in (0, 10) and radius 1, we can add to our code a call like this:

Methods in .NET Part 1

The resulting output would be the following:

Methods in .NET Part 1

In the next part we will explain parameters with variable number of arguments.

Osvaldo G.

Calling Osvaldo “el Doc” is no coincidence, he is part of the Inflection Point’s gurus and has been working in the company for 9 years; his degree in Computer Engineering, the numerous certifications and more than 15 years of experience in Microsoft technologies, have made of him a mentor for many and also an avid writer for Inflection Point’s blog with tips and facts that have saved more than one coder in their quest for better coding. He comes from San Nicolás de los Garza, Nuevo Leon and is a Tigres (local soccer team) fan.

Articles