Appendix F
Method Declarations

This appendix provides information about method declarations. A property procedure includes a pair of methods, which are also described here.

Methods

In C# all methods must be inside a class. The syntax for creating a method is as follows.

«attributes» «accessibility» «modifiers» return_type nameparameters»)
{
    code...
}

The following list describes the pieces of this declaration.

  • attributes—Attributes that specify extra properties for the method.
  • accessibility—One of public, internal, protected, internal protected, or private.
  • modifiers—One of new (hides a parent class’s version of the method), static (shared by all instances of the class), virtual (the method can be overridden in a descendant class), override (the method is overriding a version in an ancestor class), sealed (indicates an overriding method cannot be further overridden), abstract (defines only the method’s signature), or extern (the method is defined outside of the assembly).
  • return_type—The type of the data that the method returns. If the method doesn’t return a value, this should be void.
  • name—The name you want the method to have.
  • parameters—The method’s parameters.
  • code—The code that the method should execute.

Use a return statement to exit the method and return a value (if the method returns a value).

Property Procedures

Properties have get and set accessors. The syntax for creating a read/write property is as follows.

public data_type name
{
    get
    {
        ...
        return value;
    }
    set
    {
        ...
    }
}

To create a read-only or write-only property, simply omit the accessor that you don’t need.

To create an auto-implemented property, simply omit the body of the accessors, as in the following example.

public string Name { get; set; }

Lambda Functions and Expressions

A lambda expression is a method defined within the flow of the program’s code instead of as a separate method.

An expression lambda consists of a list of zero or more parameters, the => operator, and a single expression that evaluates to some result. For example, the following code creates an delegate named root and sets it equal to an expression lambda. It then displays the value of root(13).

Func<float, double> root = (value) => Math.Sqrt(value);
Console.WriteLine(root(13));

A statement lambda is similar to an expression lambda except it can execute multiple statements inside braces and uses the return statement to return its result. The following code demonstrates a statement lambda.

Func<int, int, int, int> middle = (v1, v2, v3) =>
    {
        // Sort the items.
        int[] values = { v1, v2, v3 };
        Array.Sort(values);

        // Return the middle item.
        return values[1];
    };
Console.WriteLine(middle(2, 3, 1));

An async lambda is a lambda expression with the async keyword added, so the program can execute it asynchronously. For example, the following code adds an asynchronous statement lambda to the countButton control’s Click event.

private void Form1_Load(object sender, EventArgs e)
{
    countButton.Click += async (button, args) =>
    {
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine(i);
            await System.Threading.Tasks.Task.Delay(1000);
        }
    };
}

Extension Methods

To make an extension method, place a static method in a static class. Add the keyword this before the method’s first parameter and give that parameter the type that you want to extend. For example, the following code defines a RemoveNonLetters string extension method.

public static class StringExtensions
{
    public static string RemoveNonLetters(this string text)
    {
        string result = "";
        foreach (char ch in text)
            if (((ch >= 'a') && (ch <= 'z')) ||
                ((ch >= 'A') && (ch <= 'Z')))
                result += ch;
            else
                result += "?";

        return result;
    }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.198.138