Learning C# Part 2: Language Fundamentals
Written By: Kevin Jordan
- 07 Jul 2006 -
Description: This series aims to take a beginning programmer to an intermediate level in C#. Topics covered in Part 2 include: C# program structure, declaring and intializing variables, characters, strings, constants, enumeration, converting between types, and expressions and operators.
- C# Program Structure
- Declaring and Using Different Variable Types
- Constants and Enumeration
- Converting Variables and Using Expressions
- Aww Man... Not Homework
Variable Types
Variables are used to store application data for use during execution. Variables have different types for the various kinds of data that they might store. Here's a list of some of the most common types:
Type | Definition | # Bytes |
byte | Integer between 0 and 255 | 1 |
short | Integer between -32768 and 32767 | 2 |
int | Integer between -2147483648 and 2147483647 | 4 |
uint | Integer between 0 and 4294967295 | 4 |
long | Integer between -9223372036854775808 and 9223372036854775807 | 8 |
bool | Boolean value; true or false | 1 |
float | Single-precision floating point value | 4 |
double | Double-precision floating point value | 8 |
decimal | Precise decimal value to 28 significant digits | 12 |
object | Base type of all other types | N/A |
char | Single Unicode character between 0 and 65535 | 2 |
string | An unlimited sequence of Unicode characters | N/A |
Declaring and Initialize Numbers
Before you can use a variable, you have to declare it. By declaring the variable you are reserving space for the values in memory. After declaration, you can then assign a value. It's possible to declare and assign a value in the same statement.
int myInteger; // this declare a variable myInteger = 4; // this assigns a value to myInteger int myNewInteger = 80; // This declares and assigns a value to myNewInteger
There are a couple important things to remember when naming your variables. First thing is, you can't use C# keywords, like you can't have an int called int. Secondly, everything in C# is case sensitive so MyInteger and myInteger are two different variables.
decimal myDecimal = 932.30; // This returns an error decimal myDecimal2 = 932.30M; // This one is OK
Whenever you initialize a decimal you have to specify a literal suffix. By default, C# thinks that any number with a decimal point is a double, adding the M at the end specifies the type of the literal to a decimal. Here is a list of literal suffixes:
Type | Suffix |
Unsigned Integer | U |
Long Integer | L |
Unsigned Long | UL |
Float | F |
Double | D |
Decimal | M |
Characters and Strings
You declare a character by enclosing it in single quotations marks while strings require double quotes.
char myChar = 'C'; // This is a character string myString = "Test string"; // This is a string
There are a few special characters which need to be represented using what's known as an escape sequence.
Escape sequence | Character Name |
\' | Single quotation mark |
\" | Double quotation mark |
\\ | Backslash |
\0 | Null |
\a | Alert |
\b | Backspace |
\f | Form feed |
\n | New line |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
Here are some examples of strings using escape sequences:
string s = "He said, \"Let\'s use quotes.\""; // Outputs as: He said, "Let's use quotes" string filename = "C:\\Temp\\myfile.txt"; // Outputs as: C:\Temp\myfile.txt
Another way to create strings containing filenames is by using a verbatim string. Verbatim strings are interpreted by the compiler exactly like they're written. The only exception is the double quote which you have to use another quote for an escape string. All you have to do to use verbatim strings is put an @ sign at the front.
string easierFilename = @"C:\Temp\myfile.txt"; //Outputs as: C:\Temp\myfile.txt string quotes = @"""Quotes"""; // Outputs as: "Quotes"