Harri's profilePainKiller´s C# CornerBlogListsNetworkMore ![]() | Help |
|
|
Usefull stuff unsortedI have a very bad memory, this blogg helps med remember som tips and tricks that will make my life as a system developer easier. Today I have this tips: XML Documentation CommentsThis was erlier integrated in Visual Studio now it´s a hidden and for many new developers maby unknown. If you compile with the switch /doc you will have nice documentation of all you comments added with /// Like this for example: /// <summary> /// This class performs an important function. /// </summary> Read more about this feature: http://msdn.microsoft.com/en-us/library/b2s063f7(VS.80).aspx C# Preprocessor DirectivesA nice feature, today I want to mark a class with the preprocessor directive #warning because I want this to be there until we investigated if the class should be deleted or not. Add a //TODO is not enough a warning is stronger and forces us to dont miss this before deployment. Read more about this here: http://msdn.microsoft.com/en-us/library/ed8yd1ha(VS.71).aspx GuidlinesRunning FxCop on your assemblys is a good practice to keep your code clean and efficient. FxCop not only finds code that break “best practice” rules it also suggest´s how to correct them. Some tips: Properties should not return arrays
Specify format provider
Download FxCop : http://www.microsoft.com/downloads/details.aspx?familyid=3389F7E4-0E55-4A4D-BC74-4AEABB17997B&displaylang=en Enum ToString not obsolete (thank god)I have wondered why intellisense in Visual Studio marks the ToString method on a Enum type as obsolete. Should we not use it? I personallyl thinks that ToString is much easier then Enum.GetName method. I found this on MSDN, the Empty ToString() is not obsolete and could safely be used it won´t dissapear in the future (should be strange since object class has it and every datatype is derived from object). Why the strange behaviour in Visual Studio then, well it´s the overloaded methods ToString(IFormatProvider) and ToString(String, IFormatProvider) that are obsolte and therefore not recomended to use. http://msdn.microsoft.com/en-us/library/system.enum.tostring.aspx A very simple object dumperstatic void Write<T>(T item) where T : class, new() A DataTable helperpublic class DBHelper public static void LoadRowWithInstanceValues<T>(T item, ref DataRow row) where T : new()
This handy piece of code helps you load a DataRow from a object or vice versa, Load a object with a DataRow. I use the get and setters in try catch block, it just loads propertys that have a matching column (name and datatype) in the DataTable the rest is simply ignored. Convert / Parse a string to an enumerated (enum) valueToday I learned something, the issue I want to solve is using a enumeration property as a kind of wrapper property against a string property (thats mapped as a DataColumn with SqlToLinq). This means a want to cast a string to a enum value and vice versa. This how I do it. public SettingDataType DataType The string property has the name PreferedDatatype Singleton Vs Static classesWhat advantages does the singleton have?Singletons preserve the conventional class approach, and don't require that you use the static keyword everywhere. They may be more demanding to implement at first, but will greatly simplify the architecture of your program. Unlike static classes, we can use singletons as parameters or objects. One more time : "we can use singletons as parameters or objects." Tree ways to do the samethingdouble sumOfAccountSaldo = 0; //Query syntax //Lambda syntax History of C# and some confusion added by MicrosoftA little history to clear up the version mess that Microsoft created when releasing .NET Framework 3.0, they should have named it 2.5 but because of this the C# language version and the Framework version is not the same. The list below describes the diffrent releases of .NET Framework and the C# language version (only major releases).
|
|
|