Chapter 4 Data Types, Variables, and Constants

What’s in This Chapter

  • Value versus reference types
  • Variable initialization
  • Widening and narrowing conversions
  • Passing parameters by value, by reference, and for output
  • Nullable types
  • Enumerations and delegates

Wrox.com Downloads for This Chapter

Please note that all the code examples for this chapter are available as a part of this chapter’s code download on the book’s website at www.wrox.com/go/csharp5programmersref on the Download Code tab.

A variable is a program element that stores a value. Some of the values that a variable might contain include a number, string, character, date, or object representing something complex such as a customer or business report.

A program uses variables to hold and manipulate values. For example, if some variables hold numbers, the program can apply arithmetic operations to them. If the variables hold strings, the program can use string operations on them such as concatenating them, searching them for particular substrings, and extracting substrings from them.

Four factors determine a variable’s exact behavior:

  • Data type determines the kind of the data the variable can hold (integer, character, string, and so forth).
  • Scope defines the code that can access the variable. For example, if you declare a variable inside a for loop, only other code inside the loop can use the variable. If you declare a variable at the top of a method, only the code in the method can use the variable.

Get C# 5.0 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.