none
Widespared C# and .Net Framework developers' mistakes RRS feed

  • Question

  • Hi,
    Please, could anybody tell me a resource about widespread C# and .Net Framework developers' mistakes?
    I give you some examples:
    1) Calling StreamReader or StreamWriter Dispose() method causes
    calling underlying (internally used) Stream Dispose() method as well
            public void DoSmth(Stream stream)
            {
                using (var reader = new StreamReader(stream))
                {
                    // do smth with reader here
                }
                /* 
                 * At this moment reader object called stream object Dispose(),
                 * cause using statement called reader object Dispose()
                 * and we can't use stream object anymore,
                 * Following code throws ObjectDisposedException
                 */
                using (var reader = new StreamReader(stream))
                {
                    // do smth with reader here
                }
            }
    2) Incorrect code using lambda expressions
    public void DoSmthElse()
            {
                var actions = new List<Action>(10);
                for (int i = 0; i < 10; i++)
                {
                    /* 
                     * Here we need to create local copy of i variable,
                     * otherwise code in foreach loop always will
                     * output only number 10
                     */
                    actions.Add(()=>Console.WriteLine(i));
                }
                foreach (var act in actions)
                {
                    act();
                }
            }
    Please, tell me, is there a resource about such devlopers' mistakes?
    Thnks,
    Ashot Muradian
    Wednesday, January 4, 2012 3:08 AM

Answers