Cheat Sheet

C# 2010 All-In-One For Dummies

From C# 2010 All-in-One For Dummies by Bill Sempf, Charles Sphar, Stephen R. Davis

C# is a Microsoft programming language used to build Windows programs, Web sites, and services. C# is primarily used with an Integrated Development Environment like Visual Studio 2010, which has templates for the most common products. Used with the .NET Framework, C# can be used to create graphics, run devices, connect to databases, and manage files.

C# Operators and Precedence

It’s not always easy to determine which C# operators take precedence over others. The following table offers a list of common C# operators and their precedence, along with their cardinality and associativity.

Precedence Operators Cardinality Associativity
High () [] . new typeof Unary Left to right
! ~ + - ++ -- (cast) Unary Left to right
* / % Binary Left to right
+ - Binary Left to right
< <= > >= is as Binary Left to right
== != Binary Left to right
& Binary Left to right
^ Binary Left to right
| Binary Left to right
&& Binary Left to right
|| Binary Left to right
?: Ternary Right to left
Low = *= /= %= += -= &= ^= |= <<= >>= Binary Right to left

C# Integer Variable Types

C# integer variables come in a variety of types and ranges. The following table sorts out the C# integer variables so you’ll always know the range and size of each.

Type Size (bytes) Range In Use
sbyte 1 –128 to 127 sbyte sb = -12;
byte 1 0 to 255 byte b = 12;
short 2 –32,768 to 32,767 short sn = -123;
ushort 2 0 to 65,535 ushort usn = 123;
int 4 –2,147,483,648 to 2,147,483,647 int n = 123;
uint 4 0 to 4,294,967,295 uint un = 123U;
long 8 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 — “a whole lot” long l = 123L;
ulong 8 0 to 18,446,744,073,709,551,615 long ul = 123UL;

C# Floating Point Variable Types

C# floating point variables come in two types: float and double. The following table compares these two types in terms of size, range, and accuracy.

Type Size (bytes) Range Accuracy In Use
float 8 1.5 x 10–45 to 3.4 x 1038 6–7 digits float f = 1.2F;
double 16 5.0 x 10–324 to 1.7 x 10308 15–16 digits double d = 1.2;

Other C# Variable Types

It’s not always easy to sort out C#’s variables. The following table offers a comparison of all C# variable types except integer and floating point, which are covered elsewhere.

Type Range In Use
decimal Up to 28 digits decimal d = 123M;
BigInteger NA Too humongous to list.
char 0 to 65,535 (codes in the Unicode character set) char x = 'c';
char y = '\x123';
char newline = '\n';
string From Empty (“”) to a very large number of characters in the Unicode character set string s = "my name";
string empty = "";
bool True and False bool b = true;
Dynamic Determined at runtime Dynamic f = foo()

Controlling Program Flow in C#

The following code segment depicts the great variety of ways in which program flow can be re-routed in C#, including if-else structures, while loops, and for/foreach loops.

if (i < 10)
{
  // go here if i is less than 10
}
else
{
  // go here otherwise
}
while(i < 10)
{
  // keep looping through here as long as i is less than 10
}
for(int i = 0; i < 10; i++)
{
    // loop 10 times
}
foreach(MyClass mc in myCollection)
{
  // ... execute once for each mc object in myCollection
}

Defining a Class in C#

In C#, as in most object-oriented programming languages, a class is a bundling of unlike data and functions that logically belong together into one tidy package. Good classes are designed to represent concepts. Classes are central to C# programming. In broad terms, here is how you define a class in C#:

 [access][<abstract | sealed>]class MyClassName [: [BaseClass] [, Interface, ...]]
{
  [static][access]type dataMember;
  [<static|virtual|abstract|new|override>][access]type method(. . . args . . .)
}
for classes, access is public|protected|internal|private
  for class members, access can also be protected internal

Notes:

[feature] feature is optional
<feature1 | feature2> Either feature1 or else feature2
. . . Unspecified number of statements or expressions
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com