Category Archives: C#

Find the Previous Method Called

To determine what the previous method called was prior to your current location in the code, try the following: string previousMethod = new System.Diagnostics.StackFrame(1, false).GetMethod().Name; This is a great quick way to determine the previous method for logging purposes in … Continue reading

Posted in C# | Leave a comment

Create a Singleton in C#

Sometimes you need an object instance rather than a static class but you really want that object instance to be globally unique to your application, e.g. there can be only one.  Enter the singleton pattern: public class MySingleton { private … Continue reading

Posted in C# | Leave a comment

Determine Approximate Memory Used by an Object

Curious as to how much memory your object or list of objects is taking up?  Here’s a quick way to get an approximation: long startMem = GC.GetTotalMemory(true); SortedDictionary<string, MyClass> myDictionary = GenerateDictionary(); long endMem = GC.GetTotalMemory(true); long byteSize = endMem … Continue reading

Posted in C# | Leave a comment

Embed Resources into your .Net Projects

Worrying about file paths, locations, availability, and security of your project’s resource can be a real pain.  Fortunately, you can easily embed resources into your project’s DLL and reference them with ease from your code.  Here’s an example of how … Continue reading

Posted in C#, Visual Studio | Leave a comment

Quickly Clone a Serializable Object in C#

Here’s a quick way to get a clone of your serializable object in C#: public static object GetClone(object cloneThis) { BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms= new MemoryStream(); bf.Serialize(ms, cloneThis); ms.Flush(); ms.Position = 0; return bf.Deserialize(ms); }

Posted in C# | 5 Comments

Regex to Clean Out HTML from Text

Ahh, the power (and sometimes slight confusion) of regular expressions.  This seems to work well to remove HTML from text: string htmlstring = { some chunk o html infested text }; Regex cleanOutHtml =     new Regex(@”

Posted in C#, Regular Expressions | 1 Comment

Force a named pipes connection

To force a named pipes connection to your database, prepend the name in your connection string with “np:”. E.g.

Posted in C#, MS SQL Server | Leave a comment

Override appSettings with the file attribute

To help make applications a bit more portable, it is nice to know you can override the appSettings group in your app.config or web.config file in the following manner:         The, in the same directory, create a file userAppSettings.config, … Continue reading

Posted in C# | Leave a comment

Using predicates for Find / FindAll methods on generic List<T> in C#

The call to FindAll: myDerivedClass.FindAll(MyBaseClass.AnyWordInNameStartsWith(partialName)); Within MyBaseClass.cs: public static Predicate AnyWordInNameStartsWith(string partialName) where T:MyBaseClass, new() { return delegate(T myClass) { partialName = partialName.ToLowerInvariant(); if (myClass.Name.ToLowerInvariant().StartsWith(partialName)) { return true; } List words = new List(); string[] splWords = myClass.Name.ToLowerInvariant().Split(’ ‘); for … Continue reading

Posted in C# | Leave a comment

FireFox caches JavaScript state

Firefox uses in memory caching for JavaScript and can cause strange errors in your MSFT ASP.NET Ajax applications. Try adding this to your load event to prevent those errors: if (Request.Browser.MSDomVersion.Major == 0) // Non IE Browser?) { Response.Cache.SetNoStore(); // … Continue reading

Posted in C#, FireFox, JavaScript | Leave a comment