Professional C# 6 and .NET Core 1.0. Christian Nagel

Чтение книги онлайн.

Читать онлайн книгу Professional C# 6 and .NET Core 1.0 - Christian Nagel страница 31

Professional C# 6 and .NET Core 1.0 - Christian Nagel

Скачать книгу

which is a bool in this code snippet:

      Invoking Methods

      The following example illustrates the syntax for definition and instantiation of classes, and definition and invocation of methods. The class Math defines instance and static members (code file MathSample/Math.cs):

      The Program class makes use of the Math class, calls static methods, and instantiates an object to invoke instance members (code file MathSample/Program.cs);

      Running the MathSample example produces the following results:

      As you can see from the code, the Math class contains a property that contains a number, as well as a method to find the square of this number. It also contains two static methods: one to return the value of pi and one to find the square of the number passed in as a parameter.

      Some features of this class are not really good examples of C# program design. For example, GetPi would usually be implemented as a const field, but following good design would mean using some concepts that have not yet been introduced.

      Method Overloading

      C# supports method overloading – several versions of the method that have different signatures (that is, the same name but a different number of parameters and/or different parameter data types). To overload methods, simply declare the methods with the same name but different numbers of parameter types:

      It’s not just the parameter types that can differ; the number of parameters can differ too, as shown in the next example. One overloaded method can invoke another:

      NOTE With method overloading, it is not sufficient to only differ overloads by the return type. It’s also not sufficient to differ by parameter names. The number of parameters and/or types needs to difffer.

      Named Arguments

      Invoking methods, the variable name need not be added to the invocation. However, if you have a method signature like the following to move a rectangle

      and you invoke it with the following code snippet, it’s not clear from the invocation what numbers are used for what:

      You can change the invocation to make it immediately clear what the numbers mean:

      Any method can be invoked using named arguments. You just need to write the name of the variable followed by a colon and the value passed. The compiler gets rid of the name and creates an invocation of the method just like the variable name would not be there – so there’s no difference within the compiled code.

      You can also change the order of variables this way, and the compiler rearranges it to the correct order. The real advantage to this is shown in the next section with optional arguments.

      Optional Arguments

      Parameters can also be optional. You must supply a default value for optional parameters, which must be the last ones defined:

      This method can now be invoked using one or two parameters. Passing one parameter, the compiler changes the method call to pass 42 with the second parameter.

      NOTE Because the compiler changes methods with optional parameters to pass the default value, the default value should never change with newer versions of the assembly. With a change of the default value in a newer version, if the caller is in a different assembly that is not recompiled, it would have the older default value. That’s why you should have optional parameters only with values that never change. In case the calling method is always recompiled when the default value changes, this is not an issue.

      You can define multiple optional parameters, as shown here:

      This way, the method can be called using 1, 2, 3, or 4 parameters. The first line of the following code leaves the optional parameters with the values 11, 22, and 33. The second line passes the first three parameters, and the last one has a value of 33:

      With multiple optional parameters, the feature of named arguments shines. Using named arguments you can pass any of the optional parameters – for example, this example passes just the last one:

      NOTE Pay attention to versioning issues when using optional arguments. One issue is to change default values in newer versions; another issue is to change the number of arguments. It might look tempting to add another optional parameter as it is optional anyway. However, the compiler changes the calling code to fill in all the parameters, and that’s the reason earlier compiled callers fail if another parameter is added later on.

      Variable Number of Arguments

      Using optional arguments, you can define a variable number of arguments. However, there’s also a different syntax that allows passing a variable number of arguments – and this syntax doesn’t have versioning issues.

      Declaring the parameter of type array – the sample code uses an int array – and adding the params keyword, the method can be invoked using any number of int parameters.

      NOTE Arrays are explained in detail in Chapter 7, “Arrays and Tuples.”

      As the parameter of the method AnyNumberOfArguments is of type int[], you can pass an int array, or because of the params keyword, you can pass one or any number of int values:

      If arguments of different types should be passed to methods, you can use an object array:

      Now it is possible to use any type calling this method:

      If the params keyword is used with multiple parameters that are defined with the method signature, params can be used only once, and it must be the last parameter:

      Now that you’ve looked at the many aspects of methods, let’s get into constructors, which are a special kind of methods.

      Constructors

      The syntax for declaring basic constructors is a method that has the same name as the containing class and that does not have any return type:

Скачать книгу