C# 2.0
The following loop will give you access to a list of all time zones on the local system machine. foreach (TimeZoneInfo tzi in TimeZoneInfo.GetSystemTimeZones())
{
// Do stuff with tzi here
}
Here is a simple way to get the week number given a certain date and time. int week = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
dt, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday);
For some unknown reason you can’t do a TryParse on nullable types or should I say the out parameter of a TryParse can not be nullable? For example this just won’t work. DateTime? dt;
DateTime.TryParse("12/12/2010", out dt);
Error 4 The best overloaded method match for 'System.DateTime.TryParse(string, out System.DateTime)' has some invalid arguments
The solution or workaround is to use two variables to accomplish the goal, here is an example.
DateTime? dt1;
DateTime dt2;
dt1 = DateTime.TryParse("12/12/2010", out dt2) ? dt2 : (DateTime?)null;
Here is a very simple way to resize an image using a bitmap as a canvas and the graphics object as a the resizing tool. Don’t forget to reference System.Drawing. /// <summary>
/// Resizes an image from a source file to a destination file
/// the destination file will contain the exact image at the size specified
/// </summary>
/// <param name="sourceImagePath">the source image file that needs to be resized</param>
/// <param name="destinationImagePath">the destination path that the source
/// image file needs to be resized to</param>
/// <param name="width">the new width</param>
/// <param name="height">the new height</param>
static void ResizeImage(string sourceImagePath,
...
One of the most efficient and non-intrusive ways you can implement an intercepting procedure is via an HttpModule. An HttpModule simply receives every request going to or coming from IIS and it allows you to manipulate the content as you see fit and pass it along. I wanted to spit out the time it takes from receiving a request to writing the response on one or more web pages and there was no other better way of doing it except writing an HttpModule, it toke me all but 10mins. Here is the code, feel free to blow it up. ...
/// <summary>
/// using reflection to create an instance of a generic type
/// </summary>
/// <returns>T as the generic type</returns>
private static T GetNewObject()
{
try
{
return (T)typeof(T).GetConstructor(new Type[] { }).Invoke(new object[] { });
}
catch...
As my forever quest for simple & reusable code keeps growing, I stumble across some bottlenecks once in a while. Anyway, I have a simple class that is called "PopulateObjectFromFormRequest" and as descriptive as the name is, it does one simple yet helpful task. Given an object as a generic type I.e (user) and a web form as a parameter I.e (aspnetform), it will scan through all the controls of that web form and use reflection on the object and assign values to the object properties that matches controls from within the form. How does this help me? Well if I my...
Once you start building applications that run every so often for an undefined/unknown amount of time, its very possible that two processes may overlap. For example, if you have a schedule task that executes a console application every 5 mins, it is possible that very much possible that it will execute another before the previous one completes. So, how do we determine if a process is already running? So far I've seen three ways of doing it. Use the Singleton pattern Use Mutex Use System.Diagnostics (my preffered method) /// <summary>
/// Determines if this process is already running, if so it will kill itself.
/// </summary>
static void ExitIfRunning()
{
...
Ok, seriously, this debate must end! I strongly believe that changing a variable to a property is breaking a change! However, others feel otherwise. Update: Yes, this debate is not valid for C# 3.0, however, the question is intended for the earlier versions of C# which is still widely used.private string name;
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}
How many times have you seen a similar snippet just like...
This is one of those "Yeah I've always seen people use it differently but I don't know why!" questions. Re-throwing exceptions can be misused, although it may not cause any harm to your application - there are multiple ways of re-throwing an exception, most likely for the purpose of bubbling it up to a higher level. Note to Java developers: It is different in your world. Lets look at how many ways we can use throw: throw throw ex throw new Exception(); You should not use #3 except if you are throwing a specific exception other...
Full C# 2.0 Archive
Next entries »