C# Method Tips

Array Collections File String Windows VB.NET Algorithm ASP.NET Cast Class Compression Convert Data Delegate Directive Enum Exception If Interface Keyword LINQ Loop Method .NET Number Regex Sort StringBuilder Struct Switch Time Value

Method

In the 1960s structured programming changed everything. It decomposed programs into many separate procedures. These procedures, also called methods, reference each other, influence the flow of control, yet remain separate. We introduce methods in the C# language. We show how you can declare them, call them and optimize them.

A procedure is a pattern for the local evolution of a computational process. Abelson & Sussman, p. 31

Methods

First, this program demonstrates both static and instance methods. The static void Main() method is automatically run when the program begins. In Main, we create an instance of the Example class, and then invoke the instance method Method() which outputs some text to the console.

Program that uses an instance method [C#]

using System;

class Example
{
    public void Method()
    {
	Console.WriteLine("Awesome");
    }
}

class Program
{
    static void Main()
    {
	Example example = new Example();
	example.Method();
    }
}

Output

Awesome
Examples

Modifiers. There are many different method types in the C# language. To start, we describe and provide examples for method keywords. These modifiers can be used on methods or on formal parameters to change behavior.

New Override Private Public Static Virtual

Concepts. Continuing on, we see that some method types—such as overloaded methods—do not rely on keywords. Additionally, recursive methods are simply methods that call themselves.

Extension Method Generic Method Overload Method RecursionRef keyword

Parameters. When you call a method, you pass arguments to it, and these are received as formal parameters in the method body. Here, we cover both arguments and parameters in the C# language.

Parameter Passing, Ref and Out Params Named Parameters Optional Parameters Out Ref

Arguments versus parameters. What is the difference between arguments and parameters and why is this important? This question is best answered by studying computer science textbooks, but if that is not practical right now, this example might help.

Arguments Versus ParametersReturn keyword

Return values. Often, methods return a specific value. If they do not return anything, they can be specified as a void method. We describe how you can specify that methods return certain values.

Return Void Multiple Return Values

Variables

The C# programming language

Variables in the C# language are found at the level of the method. These are also called local variables. Whenever a method is invoked, a special region on the stack is used to store all the memory locations for the local variables.

We often need local variables in our procedures other than those that have been bound as formal parameters.
Abelson & Sussman, p. 63

Program that uses local variables [C#]

using System;

class Program
{
    static void Main()
    {
	int first = 1, second, third;
	second = third = 2;

	Console.WriteLine(first);
	Console.WriteLine(second);
	Console.WriteLine(third);
    }
}

Output

1
2
2
Note (please read)

Description. This program introduces variables of the int type. You can see how the comma can be used to separate variable declarations, and variables can be assigned either at declaration, or in statements. Assignment statements may be compound as well.

Local variables. Most variables you will use in C# programs will be local variables. You can assign multiple variables at a time. Be warned, though, that you cannot exceed 65534 local variables in a single method! Another tip is that you can use local variables to copy the data from fields as an optimization.

Assign Local Variables Definite Assignment Analysis Locals Allowed Multiple Local Variable Declarations VarNull keyword

Null. The null literal indicates a reference that points to no object. You will need to deal with null in many different program contexts. It is one of the most common causes of exceptions in C# programs.

Null

Global variables. We provide an overview for using global variables in the C# language. Typically, you should use static variables for this purpose, as demonstrated.

Global Variable

Dynamic

.NET Framework information

The newest versions of the .NET Framework introduce support for the Dynamic Language Runtime. This layer is built on top of the Common Language Runtime and is used for features such as the keyword dynamic.

Dynamic

Performance

Performance optimization

How can you make method calling faster in some cases? We cover various aspects of method performance in the C# language and the .NET Framework. Performance is tested with the use of benchmarks.

Inline Optimization Local Variable Field Optimization Method Call Depth Performance Method Parameter Performance and Registers Method Size Test Parameter Optimization Tip

Misc.

Miscellaneous

Sometimes, it is helpful to take a step back and look at programming from another perspective, not drawing on what we know but rather looking at what we don't. For example, variable assignment is one way you can see how time affects space.

Comment Program Spacetime

Summary

If you have a lot of spare time, it's sometimes interesting to rewrite a program that uses methods with one that uses goto statements and labels instead. This exercise alone will really drive home the value of structured programming.

Dot Net Perls