csharp Using block

public static string GetName()
{
	try
	{
		using (DataClassesDataContext db = new DataClassesDataContext("connection-string-here"))
		{
			var query = (
				from m in db.MyTable
				where !m.Archived
				select m.Name
			);

		   return query.FirstOrDefault() ?? "";
		}
	}
	catch
	{
		return "";
	}
}
Above is optimal, as the using disposes any objects, even if an exception is thrown. Then the catch ensures the exception is caught and a value is returned.

Updated: Wednesday 4th January 2012, 12:59pm

There are 0 comments

Leave a comment of your own

Comments are currently closed.