SharePoint world of ECM and Information Management

.NET Tips & Tricks: Common

back to content

 

This section describes the common .NET tips which don't relates to the specific category.

 

INFO:

  • const is "baked" into the assembly.
If you have to change the const value in one of your dependent assemblies you need to recompile your dependent assemblies. To avoid this consider using readonly field, because it's fetched at run-time only and no re-compilation is required
  • Data members, in comparison with properties, are not binary compatible - when you change a public data member you must recompile all code that uses the public data member.
  • Diagnostics.Debugger.IsAttached() allows to check if application is running in IDE.
  • Inlining prerequisites: methods are small (32bytes of IL), no virtual, simple control flow, no try/catch, no structures for arguments or local. 
  • C# doesn't support returning structures by ref and thus we get copy of the structure, not the initial structure.
  • UTF7 file is a valid ANSII file. It's impossible to detect text encoding.
  • Anonymous methods cannot access ref/out parameters. >>
  • If an instance method (non-static) of class is called that doesn't use any instance members, that instance remains rooted and cannot be garbage collected. >>
  • ReferenceEquals( "", string.Empty ) returns true in .net 1.0/1.1 and false in 2.0. The reason is due to the [CompilationRelaxations] attribute of mscorlib which doesn't intern your strings. >>
  • Consider using the generic collections whenever it possible. The reason is that in new smaller Framework SKUs the non-generic collections are obsolete, for example in SilverLight. >>
  • XmlSerializer may lead to the leak, when root element of serialized class is changed without changing the class name. >>  

  • new Consider using the "yield return <variable>" with IEnumerable<T> whenever you are returning the collection from your methods. In this case you shouldn't keep all collection in memory and don't need to proceed to the end of list, becase the "yield" returns value immediately to the caller. >>

TIPS:

  • Equal should be overridden in structures to gain performance, because standard realization uses reflection.
  • Use throw instead of throw ex to propagate an exception, because MSIL translate throw ex to the additional empty throw exception. Alternately, use InnerException.
  • Using return statement within try{} with finally{} returns the cached original value, albeit variable will change. It means that in case of static variable the returned result will be the same, but variable value will be changed. >>
  • Prefer Using Foo(...) instead of Foo(System.Type, ...) to check casting in compile time. >>
  • There some cases where you need to implement your own add and remove accessors for an event: to provide your own underlying data store; to provide custom synchronization mechanism; execute custom logic any time a delegate is registered or unregistered from the event(for example in case of CER). >>
  • Use MemoryFailPoint class which attempts to predict whether a memory allocation will fail. >>
  •  LosFormatter is like the BinaryFormatter but is highly optimized for strings, arrays, and hash tables.
  • The prefered way to cast one object to another is using as operator in lieu of is, because in case of is pattern IL calls castclass operation twice. Fists time with isinst operation, second with explicit () casting. >>
  •  Decorator pattern allows you to implement progress bar for the BinaryFormatter. >>
  • The length of the fully-qualified file name or directory name path is 260 characters (MAX_PATH). To avoid this limit use prefix the file name with "\\?\" and call the Unicode versions of the Windows APIs, then you can use file names up to 32K characters in length. >>
  •  Take into account "Func-Eval" feature of VS when implements your getter/setters (avoid using p/invoke and etc). >>
  • C# doesn't support IL inlining, but it's possible to do in post-compiling process. >>
  • Consider using  [InternalsVisibleTo] attribute to provide the "friendship" between assemblies, which also simplifies unit-testing of the private members
  • Prefer using the NetDataContractSerializer when serializing the complex data (generic directories and etc) into XML.
NetDataContractSerializer gives you a more flexible way in serializing your collections, type instances into XML. This class includes CLR info to the serialized XML.
  • String Equals() != String.Compare()

These two methods are doing the same things - compare the strings, but with differen approach. String.Equal() performs an ordinal string comparing, where two strings are compared byte-to-byte, and where to culture info is taken into account. String.Compare performs culture-sensitive matching, with can be tuned with StringComparison param. >>

 

back to content

 

Leave a Comment

(required) 

(required) 

(optional)

(required)