CHAPTER 4
Core C# Programming Constructs,
Part II
This chapter picks up where the Chapter 3 left off, and completes your investigation of the core aspects
of the C# programming language. We begin by examining various details regarding the construction of
C# methods, exploring the out, ref, and params keywords. Along the way, you will also examine the role
of optional and named parameters.
After you examine the topic of method overloading, the next task is to investigate the details behind
manipulating arrays using the syntax of C# and get to know the functionality contained within the
related System.Array class type.
In addition, this chapter provides a discussion regarding the construction of enumeration and
structure types, including a fairly detailed examination of the distinction between a value type and a
reference type. We wrap this up by examining the role of nullable data types and the ? and ?? operators.
After you have completed this chapter, you will be in a perfect position to learn the object-oriented
capabilities of C#, beginning in Chapter 5.
Methods and Parameter Modifiers
To begin this chapter, let’s examine the details of defining methods. Just like the Main() method (see
Chapter 3), your custom methods may or may not take parameters and may or may not return values to
the caller. As you will see over the next several chapters, methods can be implemented within the scope
of classes or structures (as well as prototyped within interface types) and may be decorated with various
keywords (e.g., static, virtual, public, new) to qualify their behavior. At this point in the text, each of
our methods has followed the following basic format:
// Recall that static methods can be called directly
// without creating a class instance.
class Program
{
// static returnType MethodName(paramater list) { /* Implementation */ }
static int Add(int x, int y){ return x + y; }
}
While the definition of a method in C# is quite straightforward, there are a handful of keywords that
you can use to control how arguments are passed to the method in question. These are listed in
Table 4-1.
121