Knowledge Base/C Sharp/General
Enabling column guidelines in Visual Studio .NET text editor
It is often useful to have an indication of the right margin in the IDE editor. Eclipse IDE supports this feature. In Visual Studio .NET it is available but not documented.
To enable the guidelines, shut down Visual Studio .NET if it is currently running. Under
create a string value called Guides. Set guides to the following
where x, y, z are the RGB values and n is the column number. You can define at most 13 guidelines.
will place a red guideline after the 4th and 75th columns.
You can now launch Visual Studio and open a text file.
To disable the guidelines, simply delete the Guides value.
This works in Visual Studio .NET 2002, 2003 and 2005. It has been tested in Visual Studio .NET 2003 and 2005.
Preventing the console window from closing after debugging
If your C# project is a Console Application, the console window closes as soon as the debugging (F5 or Debug > Start Debugging) is over. This is very inconvenient, since almost certainly you will want to inspect the application's output. There are two solutions:
Solution 1: Run without debugging
Instead of pressing F5 or selecting Debug > Start Debugging, press Ctrl+F5 or select Debug > Start Without Debugging. When done, you will be asked to...
Press any key to continue . . .
Solution 2: Console.ReadKey()
You may force the program to wait until a key is pressed on the keyboard by adding
to the end of the program.
Changing the location of the Visual Studio work directory
Select Tools > Options. In the tree on the left select Projects and Solutions > General. Then modify
- Visual Studio projects location
- Visual Studio user project templates location
- Visual Studio user item templates location.
Processing an array of unknown rank (number of dimensions)
Simply use foreach:
The number of dimensions of array is available via the property array.Rank.
The standard instance Equals() method
It is generally a good idea to override ValueType.Equals() for all value types. You should also override Object.Equals() when you want to change the default semantics for a reference type. Here is the standard pattern:
where CompareMembers is a private method that performs a member-wise comparison.
Functions with variable arguments
Functions with variable arguments, also known as variadic functions, or functions of variable arity, are available in C# via the following syntax:
If invoked as follows,
this method will produce the following output:
Paul 4 7 1982
- The variable arguments don't have to be of type object. I could have defined WriteAll as follows:
- The variable arguments introduced using the params key words may follow one or more "normal" arguments provided that (1) there is only one params keyword in a method declaration and (2) it comes last on the argument list:
Built-in exceptions in C#
Here is a partial list of built-in exceptions in C#. Avoid defining your own exception types; use built-in exceptions whenever possible.
If you are designing an application that needs to create its own exceptions, extend Exception It was originally thought that custom exceptions should derive from ApplicationException to distinguish them from those thrown by the common language runtime. However, in practice this has not been found to add significant value.
The most important and frequently used exceptions are shown in bold.
- System.AccessViolationException - there is an attempt to read or write protected memory.
- System.AppDomainUnloadedException - an attempt is made to access an unloaded application domain.
- System.ApplicationException - a non-fatal application error occurs.
- System.ArgumentException - one of the arguments provided to a method is not valid.
- System.ArgumentNullException - a null reference is passed to a method that does not accept it as a valid argument.
- System.ArgumentOutOfRangeException - the value of an argument is outside the allowable range of values as defined by the invoked method.
- System.ArithmeticException - errors in an arithmetic, casting or conversion operation.
- System.ArrayTypeMismatchException - an attempt is made to store an element of the wrong type within an array.
- System.BadImageFormatException - the file image of a DLL or an executable program is invalid.
- System.CannotUnloadAppDomainException - an attempt to unload an application domain fails.
- System.ContextMarshalException - an attempt to marshal an object across a context bounday fails.
- System.DataMisalignedException - a unit of data is read from or written to an address tht is not a multiple of the data size. This class cannot be inherited.
- System.DivideByZeroException - there is an attempt to divide an integral or decimal value by zero.
- System.DllNotFoundException - a DLL specified in a DLL import cannot be found.
- System.DuplicateWaitObjectException - an object appears more than once in an array of synchronization objects.
- System.EntryPointNotFoundException - an attempt to load a class fails due to the absence of an entry method.
- System.Exception - errors that occur during application execution. The base exception class representing a generic exception.
- System.ExecutionEngineException - there is an internal error in the execution engine of the common language runtime. This class cannot be inherited.
- System.FieldAccessException - there is an invalid attempt to access a private or protected field inside a class.
- System.FormatException - the format of an argument does not meet the parameter specifications of the invoked method.
- System.IndexOutOfRangeException - an attempt is made to access an element of an array with an index that is outside the bounds of the array. This class cannot be inherited.
- System.InsufficientMemoryException - a check for sufficient available memory fails. This class cannot be inherited.
- System.InvalidCastException - invalid casting or explicit conversion.
- System.InvalidOperationException - a method call is invalid for the object's current state.
- System.InvalidProgramException - a program contains invalid Microsoft intermediate language (MSIL) or metadata. Generally this indicates a bug in the compiler that generated the program.
- System.MemberAccessException - an attempt to access a class member fails.
- System.MethodAccessException - there is an invalid attempt to access a private or protected method inside a class.
- System.MissingFieldException - there is an attempt to dynamically access a field that does not exist.
- System.MissingMemberException - there is an attempt to dynamically access a class member that does not exist.
- System.MissingMethodException - there is an attempt to dinamically access a method that does not exist.
- System.MulticastNotSupportedException - there is an attempt to combine two delegates based on the Delegate type instead of the MulticastDelegate type. This class cannot be inherited.
- System.NotFiniteNumberException - a floating-point value is positive infinity, negative infinity, or Not-a-Number (NaN).
- System.NotImplementedException - a requested method or operation is not implemented.
- System.NotSupportedException - an invoked method is not supported, or there is an attempt to read, seek, or write to a stream that does not support the invoked functionality.
- System.NullReferenceException - there is an attempt to dereference a null object reference.
- System.ObjectDisposedException - an operation is performed on a disposed object.
- System.OperationCanceledException - thrown upon cancellation of an operation that the thread is executing.
- System.OutOfMemoryException - there is not enough memory to continue the execution of a program.
- System.OverflowException - an arithmetic, casting, or conversion operation in a checked context results in an overflow.
- System.PlatformNotSupportedException - a feature does not run on a particular platform.
- System.RankException - an array with the wrong number of dimensions is passed to a method.
- System.StackOverflowException - the execution stack overflows because it contains too many nested method calls (e.g. in the case of infinite recursion). This class cannot be inherited.
- System.SystemException - the base class for predefined exceptions in the System namespace.
- System.TimeoutException - the time alotted for a process or operation has expired.
- System.TypeInitializationException - a wrapper around the exception thrown by the class initializer. This class cannot be inherited.
- System.TypeLoadException - type-loading failures occur.
- System.TypeUnloadedException - an attempt to access an unloaded class.
- System.UnauthorizedAccessException - the operating system denies access because of an I/O error or a specific type of security error.
- System.UriFormatException - an invalid Uniform Resource Identifier (URI) is detected.
- System.IO.DirectoryNotFoundException - part of a file or directory cannot be found.
- System.IO.DriveNotFoundException - trying to access a drive or share that is not available.
- System.IO.EndOfStreamException - reading is attempted past the end of a stream.
- System.IO.FileFormatException - an input file or a data stream that is supposed to conform to a certain file format specification is malformed.
- System.IO.FileLoadException - a managed assembly is found but cannot be loaded.
- System.IO.FileNotFoundException - an attempt to access a file that does not exist on disk fails.
- System.IO.InternalBufferOverflowException - the internal buffer overflows.
- System.IO.InvalidDataException - a data stream is in an invalid format.
- System.IO.IOException - an I/O error occurs.
- System.IO.PathTooLongException - a pathname or filename is longer than the system-defined maximum length.
- System.IO.PipeException - an error occurs within a named pipe.
Viewing the MSIL (CIL) of your C# project
The Microsoft Intermediate Language (MSIL), nowadays more commonly known as Common Intermediate Language (CIL), is the assembly language output by .NET compilers. You can examine the intermediate language produced by your C# compiler using the Intermediate Language Disassembler (IL DASM).
If you installed Visual Studio under C:\Program Files, ildasm.exe will be found in
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\
Run it. In IL DASM menu, select File > Open. IL DASM allows you to open files of the following types:
- *.exe - executable
- *.dll - dynamic link library
- *.mod - module
- *.mdl - memory descriptor list
Browse to the output file produced by your build. This would normaly be under your <Visual Studio projects location>\<project name>\<project name>\bin\Debug for debug code and ...\Release for release code.
IL DASM represents the code as a tree which you can visually inspect. Double-click on the nodes to view the intermediate language code. It looks as follows:
- This page was last modified on 6 September 2008, at 10:46.
- This page has been accessed 18,087 times.