PROWAREtech
.NET C# Tutorial - A Beginner's Guide - Page 2
Keywords
Keywords cannot be used as names for a variable, method or class. The C# keywords are:
abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long nameof namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using var virtual volatile void while yield
Data Types
C# is a strongly typed language, meaning that all operations are type-checked by the compiler for type compatibility. To do this all values, expressions and variables have a type. This prevents errors and increases reliability.
Integer
type | bits | range |
---|---|---|
byte | 8 | 0 to 255 |
sbyte | 8 | -128 to 127 |
short | 16 | -32,768 to 32,767 |
ushort | 16 | 0 to 65,535 |
int | 32 | -2,147,483,648 to 2,147,483,647 |
uint | 32 | 0 to 4,294,967,295 |
long | 64 | (can fit any number) |
ulong | 64 | (can fit any number) |
Floating-Point
There are two floating-points: double
and float
. double
is 64 bits while float
is 32 bits.
float
has significant rounding errors and double
is better but the next paragraph introduces decimal
.
decimal
decimal
is 128 bits and is designed to handle monetary calculations. As such, it does not exhibit the rounding errors of its
smaller cousins.
Why so Many Number Data Types?
Couldn't they have simply created one number data type that could accommodate all numbers? No, this way, a program is more efficient.
bool
bool
represents true or false values. If used to C/C++, 0 does not convert to false and 1 does not convert to true in C#.
char
char
is for single Unicode character. It is 16 bits.
object
object
can refer to an object of any other type.
string
string
is a series of Unicode characters. Every object (which is everything
in the C# language) has a ToString()
method which means that everything has a way of representing itself as a string.
var
var
is special in that it is changed to another type based on the value that is assigned to it. For example:
var num = 132; // is the same as: int num = 132;
var str = "hello"; // is the same as: string str = "hello";
Array
Arrays are collections of objects usually of the same type but not neccessarily. The following demonstrates creating and initializing a simple array of numbers, strings and then objects. Technically, an array is a data structure, not a data type.
int[] num = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] num = new int[50]; // this creates an array of 50 elements
string[] str = { "this", "is", "a", "test" };
object[] obj = { "ABC", 10, true, false, 3.33, "XYZ" };
Literals
Literals are fixed values. 1000 is a number literal and "this is text" is a string literal. 0x0 is hexadecimal for 0 and 0xFF is hexadecimal
for 255. 10L is a long
literal. 10U is an uint
literal. 10UL is an ulong
literal. 10f is a
float
literal. 10m is a decimal literal. 'c' is a char
literal.
Character Escape Sequences
Special characters need escape sequences to show.
escape sequence |
character |
---|---|
\n | newline |
\r | carriage return |
\b | backspace |
\f | formfeed |
\t | horizontal tab |
\v | vertical tab |
\\ | backslash |
\0 | null character |
\' | single quote |
\" | double quote |
String Literals
Verbatim String Literal
A verbatim string literal begins with a @ and using it can make code hard to read except for using it with file paths when there are a lot of backslashes. See next code example.
Everything is an Object
In C#, everything, absolutely everything (including literals), is an object with at least some methods. Later in this guide, classes, which define objects, will be covered. But for the purposes of this tutorial, there will be a distinction made between classes/objects and data types. Data types, including strings, are passed by value where as objects are passed by reference which means a copy of them is not made. Arrays are passed by reference. More on this in the class methods section.
The following example code demonstrates using the ToString()
method to convert both number
variables and literals to a string.
Example Program Code
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
char ch = '\''; // this assigns a single quote to a char datatype
Console.WriteLine(ch);
// this writes 5 lines
Console.WriteLine("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");
// this writes 5 more lines (verbatim string literal)
Console.WriteLine(@"Line 6
Line 7
Line 8
Line 9
Line 10");
// double quotes
Console.WriteLine("\"inside double quotes\"");
// verbatim string literal
Console.WriteLine(@"His name is ""John""");
int i = 123;
// write the hexadecimal value of i using ToString()
Console.WriteLine(i.ToString("X2"));
// write the hexadecimal value of 254 using ToString()
Console.WriteLine((254).ToString("X2"));
}
}
}