C# tips


Value type specifics

bool accepts only true and false - no numeric conversions (C days are gone)
char is 16 bits
byte is a native type - use it if you really need 8 bits.
string is a native type

Arrays - not just a bunch of values

Arrays are first class objects in C#. As such they are much more powerful entities than C arrays.
You can get dimension infomration form ana array.Arrays can perform sorting and cloning.
Declaration samples:
Single-dimensional arrays:
int[] numbers;
Multidimensional arrays:
string[,] names;
Array-of-arrays (jagged):
byte[][] scores;
Arrays allow in-place initialization with or without size specification.
MSDN Documentation

Enums - not just glorified ints

Enums are again more powerful than simple aliases for integers.
Enums in C# can convert to string representing the symbolic name, convert string with symbolic string to enum value get the list of symbolic names.
Enum values get printed as symbolic strings.
MSDN Documentation

Most common mistake for C++ refugees


One of the most common mistake for the C++ coders moving to C# is forgetting to initialize the reference variables in the class. For example:
class A {
  private B b;
  public A() {
    b.f(); // this will fail 'cause b is not initialized
  }
}
Should instead be
class A {
  private B b;
  public A() {
    b = new B();
    b.f(); 
  }
}
Or
class A {
  private B b=new B(); // good to use if you have multiple constructors but same init for this member
  public A() {
    b.f(); 
  }
}
to main .Net page

 Home    My LiveJournal    Professional    Photos    Hobbies    Friends  


Copyright (C) 2002, Ilia Tulchinsky
If you have any questions, bug reports, suggestions or just thoughts worth sharing contact me at ilia@tulchinsky.com.