Tags

, , ,

Most of my experience is based on JavaScript, which is heavily used in SharePoint 2013 development. However, C# is not quite an obsolete language for SharePoint, so I think is a good idea to review basic C# knowledge. The scope for today is to show how to call methods with parameters. Even it looks very basics, you can be sure some of the knowledge are pretty much new for the beginners.

Simple call of a method with parameters

The most simple case is to call a methods with non optional, out or ref parameters. The case is illustrated below and produce expected output, which is “Andrei Popescu”.

 class Program
    {
        
        static void Main(string[] args)
        {
            Program prg = new Program();
        }


        public Program()
        {
            Console.Write(this.SimpleMethodCalling("Andrei", "Popescu"));
            Console.ReadKey();

        }

        string SimpleMethodCalling(string firstName, string lastName)
        {
            return firstName + " " + lastName;
        }
    }

Studying this example, you can figure out the main rule when calling a method is to provide parameters respecting the order. However, this is not always the case, as you can see in the situation below.

Calling a method with named parameters

This a second possible situation you might see while you do programming in C#. If you do not want to pass parameters in the specified order, you can call the method by naming explicitly parameters. Is not something you will see often, but is good to know this is possible.

    class Program
    {
        
        static void Main(string[] args)
        {
            Program prg = new Program();
        }


        public Program()
        {
            Console.Write(this.MethodCalledWithNamedParameters(lastName: "Last name", firstName: "First name"));
            Console.ReadKey();

        }

        string MethodCalledWithNamedParameters(string firstName, string lastName)
        {
            return firstName + " " + lastName;
        }
    }

Providing out parameter to a method

If the previous case is not often used, this is. Providing out parameter to a method is representing a technique to obtain an output besides what method is returning.


    class Program
    {
        
       private string _fullName;


        static void Main(string[] args)
        {
            Program prg = new Program();
        }


        public Program()
        {
            this.MethodWithOut("First name", "Last name", out this._fullName);
            Console.Write(this._fullName);
            Console.ReadKey();

        }

        void MethodWithOut(string firstName, string lastName, out string fullName)
        {
            fullName = firstName + " " + lastName;
        }
    }

What you need to keep in mind is out parameter needs to be assigned inside the method, otherwise you will face an error, which in our case will be: “The out parameter ‘fullName’ must be assigned to before control leaves the current method”. So I think is safe to say providing an out parameter is also a good practice if you want to be sure developers working on your code will assign a value to it.

If we are discussing this case, let’s try to combine it with previous one and try to call the method with out and named parameters in the same time. Let’s try first placing the named parameters before out one.

this.MethodWithOut(firstName: "First name", lastName:"Last name", out this._fullName);

This code will fail with this error: “Named argument specifications must appear after all fixed arguments have been specified”. And this is leading us to another rule When you use named parameters combined with fixed ones, please put named arguments after all fixed parameters have been specified. The correct code will be:

// We changed the signature
void MethodWithOut( out string fullName, string firstName, string lastName)
{
   fullName = firstName + " " + lastName;
}
// We call method with named parameters
this.MethodWithOut(out this._fullName, firstName: "First name", lastName:"Last name");

Providing ref parameter to a method

In general, a ref parameter is similar with out parameter, with a small difference. When you provide a ref parameter, you are not forced to assign a value to it inside a method.

  class Program
    {
        
        
        private string _fullName = "Andrei Popescu";

        static void Main(string[] args)
        {
            Program prg = new Program();
        }


        public Program()
        {

            // We do not perform any action on ref parameter and no error occurs
            this.MethodWithRefNoAction(ref this._fullName, firstName: "First name", lastName: "Last name");
            Console.Write(this._fullName);
            // We change ref parameter
            this.MethodWithRef(ref this._fullName, firstName: "First name", lastName: "Last name");
            Console.Write(this._fullName);
            Console.ReadKey();

        }

        void MethodWithRefNoAction(ref string fullName, string firstName, string lastName)
        {

        }
        void MethodWithRef(ref string fullName, string firstName, string lastName)
        {
            fullName = firstName + " " + lastName;
        }
    }

Optional parameters

Some people say optional parameters are kept in C# to provide common development techniques with other programming languages, but is recommended to concentrate on methods overloading instead of using optional parameters. I am not sure about it, but is good to know how you can use them. Let’s assume we have this method.

string MethodWithOptional(string firstName,  string lastName = "Last name")
{
            return firstName + " " + lastName;
}

In a correct way, it can be called like this.

// Returns "Andre Last name"
this.MethodWithOptional("Andrei");
// Returns "Andre Popescu"
this.MethodWithOptional("Andrei", "Popescu");
// Returns "Andre Popescu" by providing named arguments
this.MethodWithOptional(lastName: "Popescu", firstName: "Andrei")

When you build methods to accept optional parameters, be aware out and ref parameters cannot have a default value and therefore cannot be optional. Also, place optional parameters after non-optional, otherwise your code will throw an error.

// This code will not work
        string MethodWithOptional(string firstName = "First name",  string lastName)
        {
            return firstName + " " + lastName;
        }
Advertisements