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
forloop, 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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access