Archive for C# tag
October 2010 .NET .NET C# enum
7,785 views
10 responses
Recently I needed to map external data into in-memory objects. In such scenarios the TryParse methods of Int and String are useful but where is Enum.TryParse? TryParse exists in .NET 4.0 but like a lot of people I’m on .NET 3.5.
A quick look at Enum left me scratching my head.
- Why didn’t enums receive the generic love that collections received in .NET 2.0?
- Why do I have to pass in typeof(MyEnum) everywhere?
- Why do I have to the cast results back to MyEnum all the time?
- Can I write TryParse and still make quick – i.e. without try/catch?
I found myself with a small class, Enum<T> that solved all these. I was surprised when I put it through some benchmarks that also showed the various methods were significantly faster when processing a lot of documents. Even my TryParse was quicker than that in .NET 4.0.
While there is some small memory overhead with the initial class (about 5KB for the first, a few KB per enum after) the performance benefits came as an additional bonus on top of the nicer syntax.
Before (System.Enum)
var getValues = Enum.GetValues(typeof(MyEnumbers)).OfType();
var parse = (MyEnumbers)Enum.Parse(typeof(MyEnumbers), "Seven");
var isDefined = Enum.IsDefined(typeof(MyEnumbers), 3);
var getName = Enum.GetName(typeof(MyEnumbers), MyEnumbers.Eight);
MyEnumbers tryParse;
Enum.TryParse<MyEnumbers>("Zero", out tryParse);
After (Enum<T>)
var getValues = Enum<MyEnumbers>.GetValues();
var parse = Enum<MyEnumbers>.Parse("Seven");
var isDefined = Enum<MyEnumbers>.IsDefined(MyEnumbers.Eight);
var getName = Enum<MyEnumbers>.GetName(MyEnumbers.Eight);
MyEnumbers tryParse;
Enum<MyEnumbers>.TryParse("Zero", out tryParse);
I also added a useful ParseOrNull method that lets you either return null or default using the coalesce so you don’t have to mess around with out parameters, e.g.
MyEnumbers myValue = Enum<MyEnumbers>.ParseOrNull("Nine-teen") ?? MyEnumbers.Zero;
The class
using System;
using System.Collections.Generic;
using System.Linq;
public static class Enum<T> where T : struct {
private static readonly IEnumerable<T> All = Enum.GetValues(typeof(T)).Cast<T>();
private static readonly Dictionary<string, T> InsensitiveNames = All.ToDictionary(k => Enum.GetName(typeof(T), k).ToLowerInvariant());
private static readonly Dictionary<string, T> SensitiveNames = All.ToDictionary(k => Enum.GetName(typeof(T), k));
private static readonly Dictionary<int, T> Values = All.ToDictionary(k => Convert.ToInt32(k));
private static readonly Dictionary<T, string> Names = All.ToDictionary(k => k, v => v.ToString());
public static bool IsDefined(T value) {
return Names.Keys.Contains(value);
}
public static bool IsDefined(string value) {
return SensitiveNames.Keys.Contains(value);
}
public static bool IsDefined(int value) {
return Values.Keys.Contains(value);
}
public static IEnumerable<T> GetValues() {
return All;
}
public static string[] GetNames() {
return Names.Values.ToArray();
}
public static string GetName(T value) {
string name;
Names.TryGetValue(value, out name);
return name;
}
public static T Parse(string value) {
T parsed = default(T);
if (!SensitiveNames.TryGetValue(value, out parsed))
throw new ArgumentException("Value is not one of the named constants defined for the enumeration", "value");
return parsed;
}
public static T Parse(string value, bool ignoreCase) {
if (!ignoreCase)
return Parse(value);
T parsed = default(T);
if (!InsensitiveNames.TryGetValue(value.ToLowerInvariant(), out parsed))
throw new ArgumentException("Value is not one of the named constants defined for the enumeration", "value");
return parsed;
}
public static bool TryParse(string value, out T returnValue) {
return SensitiveNames.TryGetValue(value, out returnValue);
}
public static bool TryParse(string value, bool ignoreCase, out T returnValue) {
if (!ignoreCase)
return TryParse(value, out returnValue);
return InsensitiveNames.TryGetValue(value.ToLowerInvariant(), out returnValue);
}
public static T? ParseOrNull(string value) {
if (String.IsNullOrEmpty(value))
return null;
T foundValue;
if (InsensitiveNames.TryGetValue(value.ToLowerInvariant(), out foundValue))
return foundValue;
return null;
}
public static T? CastOrNull(int value) {
T foundValue;
if (Values.TryGetValue(value, out foundValue))
return foundValue;
return null;
}
}
Usage notes
- This class as-is only works for Enum’s backed by an int (the default) although you could modify the class to use longs etc.
- I doubt very much this class is of much use for flag enums
- Casting from long can be done using the CastOrNull function instead of just putting (T)
- GetName is actually much quicker than ToString on the Enum… (e.g. Enum<MyEnumbers>.GetName(a) over a.ToString())
- IsDefined doesn’t take an object like Enum and instead has three overloads which map to the actual types Enum.IsDefined can deal with and saves runtime lookup
- Some of the method may not behave exactly like their Enum counterparts in terms of exception messages, nulls etc.
[)amien
August 2009 – August 2010 .NET .NET C#
5,776 views
8 responses
The design of a Dictionary<T> lends itself well to a caching or identification mechanism and as a result you often see code that looks like this:
private static Dictionary<string,Employee> employees;
…
public static Employee GetByName(string name) {
Employee employee;
if (!employees.TryGetValue(name, out employee)) {
employee = new Employee(whatever);
employees.Add(name, employee);
}
return employee;
}
It’s not that it is particularly difficult but it can be a bit error prone and when you’re doing it over and over. What would be nicer is something that let you do:
public static Employee GetByName(string name) {
return employees.GetOrAdd(name, () => new Employee(whatever));
}
Here’s an extension method to drop-in to a static class of your choosing that achieves just that.
public static TDictionaryValue GetOrAdd<TKey, TDictionaryValue>(this IDictionary<TKey, TDictionaryValue> dictionary, TKey key, Func<TDictionaryValue> newValue)
{
TDictionaryValue value;
if (!dictionary.TryGetValue(key, out value)) {
value = newValue.Invoke();
dictionary.Add(key, value);
}
return value;
}
[)amien
January 2009 – March 2011 .NET .NET C# Design-Patterns linq-to-sql state pattern
3,753 views
8 responses
A question I see from time-to-time on LINQ to SQL relates to changing an entity’s class.
C# and VB.NET don’t allow a class to change its type at runtime and LINQ to SQL specifically doesn’t provide a mechanism for changing the underlying discriminator for this reason.
Discarding the current object and creating a new one is fraught with issues. What do we do about existing references, unsaved data, established associations and caches?
Start with an example
Consider an abstract Account class with SavingsAccount and CurrentAccount sub-classes. Bank accounts don’t change type once created (in my experience) so that’s good so far.
When we get into processing and validation logic its tempting to create ClosedAccount and OpenAccount classes but what happens during execution when closing an account?
A further consideration is how exactly ClosedAccount and OpenAccount fit into the hierarchy given the single-inheritance limitation of C# and VB.NET.
Enter the State Pattern
The ever-resourceful Gang of Four describe the State Pattern as:
Allow an object to alter its behavior when its internal state changes.
The object will appear to change its class.
Taking the bank accounts example and applying the state pattern gives:

Account no longer needs to change type at run-time yet is still able to have clients call Validate and process Methods that end up in discrete methods as if inheritance had been used.
To achieve this we have:
- Created a State hierarchy that contains the logic we need to change at run-time
- Introduced a private member in Account that points to the current state instance
- Ensured the Account methods call the current state class via the private member
Because the state member is private and only accessed by Account itself we can happily create and dispose it as the conditions that affect the state change as much as we like without worrying about references to it.
This is best illustrated with code. Here rather than just calling the state’s validation logic there is a combination of core Account validation (balance), state validation (closed) and CheckingAccount validation (transaction limits):
public abstract class Account {
private AccountState state;
public virtual Status Validate(ITransaction tx) {
Status result = state.Validate(tx);
if (tx.Amount > Balance)
result.Add(TransactionFailures.InsufficientFunds);
return result;
}
}
public class SavingsAccount : Account {
public override Status Validate(ITransaction tx) {
Status result = base.Validate(tx);
if (Transactions.Count > TransactionLimit)
result.Add(TransactionFailures.TransactionLimitReached);
return result;
}
}
public class ClosedAccountState : AccountState {
public override Status Validate(ITransaction tx) {
return new Status(TransactionFailures.InvalidSourceAccount);
}
}
This is less complex than selectively replacing objects within our application at run-time and can bring additional benefits:
Like all guidance, patterns and principles do not blindly follow these guidelines or patterns but consider how it affects and fits with your application. For this particular example it not only solves the problem but helps maintainability – at least at this simple stage. Once Validation becomes sufficiently complex it would likely move out entirely into a new set of orchestrated classes just for that.
With LINQ to SQL (and other mappers)
Moving this example into an object-relational mapper requires two – not unexpected – database-mapped properties.
- The inheritance discriminator (Type)
- A state indicator (Active)

The only thing we need to ensure is the Account’s state member always refers to either a ClosedAccountState or OpenedAccountState depending upon the Active flag.
Given that LINQ to SQL code-generates the property for Active we could:
- Make Active private, wrap it in another property and set the state member when it changes and at initialization
- Make the state member a read-only property instead of an instance variable
The second works well here and given that AccountState is itself stateless (perhaps not the best choice of name) we can use a singleton to avoid multiple instances. The state instance variable in the Account class is replaced with:
private AccountState State {
get {
if (Active)
return OpenAccountState.Instance;
else
return ClosedAccountState.Instance;
}
}
The code continues to work and now changing the Active flag results in different behavior.
Best of all we still have the code in separate classes, no switch/case/if statements relating to validation or account types, a clean inheritance hierarchy and no running around trying to invalidate existing references.
Hitting the discriminator directly
There may be times when claims are made that a type has to change – perhaps data was entered incorrectly.
Before delving into the database or providing a tool to flip the underlying discriminator value consider:
- Does the new class interpret the same data in a different manner?
Has a $1,000 credit limit just become a 1,000 transactions per-month limit?
- Would the object be valid in the new class?
Did a ProposedCustomer just become ApprovedCustomer without a policy-enforced credit check?
- Are associations still honored?
Are 300 unshipped orders for a GameProduct still honored for a BookProduct?
If in doubt don’t do it.
An inconsistent database bleeding through your application isn’t good for anyone and will take a lot longer to sort out than setting up a new entity.
[)amien
July 2008 – April 2011 .NET C# linq-to-sql
26,278 views
11 responses
The Log property on a LINQ to SQL data context takes a TextWriter and streams out details of the SQL statements and parameters that are being generated and sent to the server.
Normally in examples you will see Console.Out being assigned to it which is fine for small demos and sandboxes but sooner or later you’ll want access to it in Windows or web applications. Here are some examples of how to redirect TextWriter output such as the DataContext log to other destinations.
To the output/debug window
The output/debug window mechanism actually uses a listener mechanism and so doesn’t actually directly expose a TextWriter interface however we can simply wrap up Debug.Write in something that does and use that instead:
class DebugTextWriter : System.IO.TextWriter {
public override void Write(char[] buffer, int index, int count) {
System.Diagnostics.Debug.Write(new String(buffer, index, count));
}
public override void Write(string value) {
System.Diagnostics.Debug.Write(value);
}
public override Encoding Encoding {
get { return System.Text.Encoding.Default; }
}
}
To use it then simply:
myDataContext.Log = new DebugTextWriter();
To a file
#if DEBUG
db.Log = new System.IO.StreamWriter("linq-to-sql.log") { AutoFlush = true };
#endif
If you wish to not overwrite the existing log file then change the constructor to include the parameter true after the filename. Bear in mind this log file can get very large and slow down your application with all that extra writing to disk and could well reveal information you’d rather wasn’t persisted there so the DEBUG conditional is recommended.
To memory
#if DEBUG
var sw = new System.IO.StringWriter();
db.Log = sw;
#endif
You will be able to examine sw or call ToString() on it to see the contents. Again this is not recommended for production as it will cause a lot of memory consumption as the StringWriter gets larger and larger.
To multiple writers
Here is a small useful class that lets you send the results intended for a TextWriter off into multiple writers.
class MulticastTextWriter : TextWriter {
private readonly IList<TextWriter> textWriters;
public MulticastTextWriter()
: this(new List<TextWriter>()) {
}
public MulticastTextWriter(IList<TextWriter> textWriters) {
this.textWriters = textWriters;
}
public void Add(TextWriter textWriter) {
lock (textWriters)
textWriters.Add(textWriter);
}
public bool Remove(TextWriter textWriter) {
lock (textWriters)
return textWriters.Remove(textWriter);
}
public override void Write(char[] buffer, int index, int count) {
lock (textWriters)
foreach (TextWriter textWriter in textWriters)
textWriter.Write(buffer, index, count);
}
public override Encoding Encoding {
get { return System.Text.Encoding.Default; }
}
}
So if you wanted to output to a log and also to the debug window, you would use it like this (again recommended only for debugging):
MulticastTextWriter mw = new MulticastTextWriter();
mw.Add(new DebugTextWriter());
mw.Add(new System.IO.StreamWriter("linq-to-sql.log") { AutoFlush = true };
db.Log = mw;
Anything you want
To wrap things up here is a small TextWriter that lets you go off and do whatever you like with the string via the Action delegate.
class ActionTextWriter : TextWriter {
private readonly Action<string> action;
public ActionTextWriter(Action<string> action) {
this.action = action;
}
public override void Write(char[] buffer, int index, int count) {
Write(new string(buffer, index, count));
}
public override void Write(string value) {
action.Invoke(value);
}
public override Encoding Encoding {
get { return System.Text.Encoding.Default; }
}
}
So if you wanted to output all log information to say a WinForms message box a tiny lambda expression gets you there:
db.Log = new ActionTextWriter(s => MessageBox.Show(s));
Have fun!
[)amien