Last Updated: 4/2002

Miscellaneous .NET Snippets


Compare Object References

To compare two object references (as close to pointers as you will get in C# outside of an unsafe block) use the Object.ReferenceEquals static function:

    if (Object.ReferenceEquals(obj1, obj2)) ....

or cast the objects to object first:

    if ( ((object) str1) == ((object) str2)) ...

Note that ReferenceEquals is implemented on the Object static object and not on the object class itself. Therefore is not available on any base class inheriting from object and therefore, for example, there are no such functions as String.ReferenceEquals(...) etc.

Back to Topic List 

Converting Enums

To convert a enum to a string, just use ToString(). To convert back use Enum.Parse(...). Enum.Parse returns a type of object, so another cast is requried.

    Enum fred = {Small,Medium,Large};
    string strEnum = fred.Small.ToString();
    fred myFred = (fred) Enum.Parse(typeof(fred), strEnum);

Back to Topic List 

Displaying an Animated GIF in Win32

Ether use the Picture control (not recommended) or the .NET Framework ImageAnimator class (simplified example below)

using System;
using System.Drawing;

WinTest {
  public class AnimatedGIF : UserControl {
private Bitmap bmpGIF;

    public AnimatedGIF() {
// Prevent flicker
        this.SetStyle(ControlStyles.DoubleBuffer |  
            ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque,

// This call is required by the Windows.Forms Form Designer.

        // Load graphic
        bmpGIF = new Bitmap(@"c:\Project\WinTest\EweBeenFlammed.gif");

        // Start playing
new EventHandler(this.OnFrameChange));

    public void OnFrameChange(object oSender, System.EventArgs e) {
// Next frame

// Signal redraw required

     protected override void OnPaint(PaintEventArgs e) {
// Draw the current frame
        e.Graphics.DrawImage(bmpGIF, 0, 0);

/// Auto-generated stuff for designer support
private System.ComponentModel.Container components = null;
protected override void Dispose( bool disposing ) {
if( disposing ) if(components != null) components.Dispose();
    base.Dispose( disposing );

Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
        components =
new System.ComponentModel.Container();


Back to Topic List 

Misc. Namespaces
  • System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
    Returns the "runtime" directory. Returns the framework directory when I try it from a console app.
  • System.Environment.CurrentDirectory [Note: This is a property, not a method)
    Returns the expected runtime directory [only tested in console app, haven't tried web app yet]
  • System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion()
    System.Environment.Version    [Note: This is a property, not a method]
    Returns the Framework version (GetSystemVersion appears to prefix a 'v', but is otherwise the same)
  • HKLM\Software\Microsoft\.NETFramework\InstallRoot
    (Unsupported) registry holding Framework installation root.

Back to Topic List 

Reading Embedded Files

Files can be embedded into the build DLL by changing setting the Build Action for the file to "Embedded Resource". Note that if the file is stored in a sub-directory this becomes a namespace and therefore a "." instead of a "/". For example, to read the embedded file originally stored as "css/HRMSControls.css" we would write the following code:

Assembly a = Assembly.GetExecutingAssembly();
Stream myStream = a.GetManifestResourceStream(a.GetName().Name + ".myEmbeddedFile.css");
StreamReader myReader = new StreamReader(myStream);
string fred = myReader.ReadToEnd();

Back to Topic List 

Write to NT Event Log

using System.Diagnostics;
string myLogSource="myApp";
if(!EventLog.SourceExists(myLogSource)) EventLog.CreateEventSource(myLogSource);

EventLog myLog = new EventLog();
Log.Source = myLogSource;
Log.WriteEntry("my log entry", EventLogEntryType.Error);

Back to Topic List