#1,171 – Lambda Expression Internals

Suppose that we have the following lambda expression (doubles a number) and that we invoke it.

    class Program
    {
        static void Main(string[] args)
        {
            Func<int, int> intDoubler = x => 2 * x;
            Console.WriteLine(intDoubler(12));
        }
    }

1171-001
We can compile this code and then use the IL Disassembler to inspect the resulting IL.

We can see that in our main class we have a static Main method and also a private static method defined by the compiler to contain our lambda expression.  In this case, it’s named <Main>b__0.

1171-002

Cracking open Main() reveals that it indeed calls <Main>b__0.

1171-003

And looking at the body of <Main>b__0, we can see that it indeed just doubles the input parameter.

1171-004

Advertisement

#235 – return Statement Is Not Needed for void Methods

If a method’s return type is void, i.e. if it does not return a value to the caller, you do not need a return statement.

        public void BarkTwice()
        {
            Console.WriteLine("Woof");
            Console.WriteLine("Woof");
        }

Control is returned to the caller automatically when the end of the function is reached.

You can actually include the return statement if you like–the compiler won’t complain–but there is no need for it.

        public void BarkTwice()
        {
            Console.WriteLine("Woof");
            Console.WriteLine("Woof");

            return;
        }

#234 – Multiple return Statements

You can have more than one return statement in a method and they can appear anywhere in the method.  If the return type of the method is not void, you must have at least one return statement and all return statements must return a value of the appropriate type.

It’s normally considered good practice to have a single return statement at the end of the method, to make the code more readable.  But you could also do something like the following:

        public float GiveAgeInHumanYears()
        {
            if (Age < 1)
                return 0;
            else if (Age == 1)
                return 10.5f;
            else
                return 21 + ((Age - 2) * 4);
        }

#233 – Returning a Result from an Instance Method

You can return a result from an instance method by declaring its return type to be something other than void.  An instance method can return a result of any type, including both value types and reference types.

In the example below, the Dog.GiveAgeInHumanYears method returns a float value.

    public class Dog
    {
        public string Name;
        public int Age;

        public float GiveAgeInHumanYears()
        {
            float ageInHuman;

            // 10.5 yrs for 1st 2 yrs, 4 yrs for each thereafter
            if (Age < 1)
                ageInHuman = 0;
            else if (Age == 1)
                ageInHuman = 10.5f;
            else
                ageInHuman = 21 + ((Age - 2) * 4);

            return ageInHuman;
        }
    }

The new value is calculated based on the value of the Age field–which stores the age of the Dog instance for which this method was called.  The calculated human age is then returned as the result, using the return statement.

Using this method:

    // Assuming that Jack is 16 and Kirby is 13
    Console.WriteLine(jack.GiveAgeInHumanYears());   // 77
    Console.WriteLine(kirby.GiveAgeInHumanYears());  // 65

#232 – Declaring and Using Instance Methods in a Class

In a class, an instance method is a kind of class member that is a block of statements that will execute when a user of the class calls the method.  It typically acts upon the data stored in that particular instance of the class.

A method has a return value that allows the method to return a result, or a return type of void, indicating that it doesn’t return any result.

Here’s a simple example of a method:

    public class Dog
    {
        public string Name;
        public int Age;

        public void BarkYourAge()
        {
            for (int i = 1; i <= Age; i++)
                Console.WriteLine("Woof");
        }
    }

Calling our method:

    Dog rover = new Dog();
    rover.Name = "Rover";
    rover.Age = 3;

    rover.BarkYourAge();

The result: