Posted by: Nick Jamil | April 26, 2009

C# Tidbits

I’m going to try to populate this post with tid-bits as I learn more about C# and the .NET BCL.

  • string is an alias for String
  • string.Empty is preferable in place of “”
  • const is equivalent ot Java’s final
  • C# uses type-inference: var s = “a string”
  • bool is equivalent to Java’s boolean
  • strings are stored using UTF-16 encoding
  • C# also has a StringBuffer class with exactly the same purpose as in Java
  • C# supports structs; they can contain much of what a class can contain
  • primitives also inherit from System.Object; hence you can say 5.ToString()
  • the keywords out and ref cause a pass-by-reference
    class OutExample {
        static void Method(out int i) {
            i = 44;
        }
        static void Main() {
            int value;
            Method(out value);
            // value is now 44
        }
    }
  •  ref requires the variable to be initialized already
  • synchronization is realized using the lock-block
  • indexers (i.e. dereferencer overriding)
    class SampleCollection<T> {   

        // Declare an array to store the data elements.
        private T[] arr = new T[100];
    
        // Define the indexer, which will allow client code to use [] notation on the class instance itself. (See line 2 of code in Main below.)
        public T this[int i] {
            get { return arr[i]; }
            set { arr[i] = value; }
        }
    }
    
    // This class shows how client code uses the indexer.
    class Program {
        static void Main(string[] args) {
            // Declare an instance of the SampleCollection type.
            SampleCollection<string> stringCollection = new SampleCollection<string>();
    
            // Use [] notation on the type.
            stringCollection[0] = "Hello, World";
            System.Console.WriteLine(stringCollection[0]);
        }
    
  • Methods can be passed around as first class functions using delegates
    public delegate void Del(string message);
    // assign a function with the same signature as the delegate to Del

    public void MethodWithCallback(int param1, int param2, Del callback) {
    callback("The number is: " + (param1 + param2).ToString());

  • namespace block encapsulates classes and acts like the Java package keyword
  • C#-docs are done using XML tags
  • classes go in namespaces go in modules go in assemblies (which are either .exe or .dll files)
  • introspection tools are in the System library
  • extend is achieved by “:”. public class A : B { … }
  • destructors are part of the language. ~MyClass() { … }
  • performance
    • boxing/unboxing is expensive
    • use System.Text.StringBuffer for String operations
    • destructors are expensive
    • don’t unnecessarily use wide data types
    • reflection is slow
    • allocate memory conservatively (e.g. avoid String.Split on large strings)
    • too many pointers spells trouble for the GC and results in internal fragmentation
    • exception throwing is expensive
    • calling a function many times for each element of (for exmaple) an array is bad; create a function to handle collections (e.g. use AddRange for collections)
    • if data is very simple, use structs instead of classes because they’re continguously stored
    • use pools for connections, free space, etc
    • cache

Leave a response

Your response:

Categories