Monthly Archives: January 2018

How to get only time from date-time C#

Questions: Suppose I have the value 6/22/2009 10:00:00 AM. How do I get only 10:00 Am from this date time. Answers: You have many options for this: DateTime dt = DateTime.Parse(“6/22/2009 07:00:00 AM”); dt.ToString(“HH:mm”); // 07:00 // 24 hour clock // hour is always 2 digits dt.ToString(“hh:mm tt”); // 07:00 AM // 12 hour clock… Read More »

Why is there no Char.Empty like String.Empty?

Questions: Is there a reason for this? I am asking because if you needed to use lots of empty chars then you get into the same situation as you would when you use lots of empty strings. Edit: The reason for this usage was this: myString.Replace (‘c’, ”) So remove all instances of ‘c’s from… Read More »

Is it appropriate to extend Control to provide consistently safe Invoke/BeginInvoke functionality?

Questions: In the course of my maintenance for an older application that badly violated the cross-thread update rules in winforms, I created the following extension method as a way to quickly fix illegal calls when I’ve discovered them: /// <summary> /// Execute a method on the control’s owning thread. /// </summary> /// <param name=”uiElement”>The control… Read More »

Is there a way to implement custom language features in C#?

Questions: I’ve been puzzling about this for a while and I’ve looked around a bit, unable to find any discussion about the subject. Lets assume I wanted to implement a trivial example, like a new looping construct: do..until Written very similarly to do..while do { //Things happen here } until (i == 15) This could… Read More »

Can somebody please explain async / await?

Questions: I’m starting to learn about async / await in C# 5.0, and I don’t understand it at all. I don’t understand how it can be used for parallelism. I’ve tried the following very basic program: using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program… Read More »

Category: C#

Can you catch a native exception in C# code?

Questions: In C# code can you catch a native exception thrown from deep in some unmanaged library? If so do you need to do anything differently to catch it or does a standard try…catch get it? Answers: You can use Win32Exception and use its NativeErrorCode property to handle it appropriately. // const int ERROR_FILE_NOT_FOUND… Read More »

Is EndInvoke() optional, sort-of optional, or definitely not optional?

Questions: I’ve read conflicting opinions as to whether every BeginInvoke() has to be matched by an EndInvoke(). Are there any leaks or other problems associated with NOT calling EndInvoke()? Answers: Delegate.EndInvoke is documented as a thou shalt call this (i.e. necessary – else leaks happen) – from msdn: Important Note No matter which technique you… Read More »

Category: C#