How do I calculate someone’s age based on a DateTime type birthday?
Given a DateTime representing a person’s birthday, How do I calculate their age in years? 70 70
Given a DateTime representing a person’s birthday, How do I calculate their age in years? 70 70
I have been running StyleCop over some C# code, and it keeps reporting that my using directives should be inside the namespace. Is there a technical reason for putting the using directives inside instead of outside the namespace? 12 There is actually a (subtle) difference between the two. Imagine you have the following code in … Read more
What is the difference between decimal, float and double in .NET? When would someone use one of these? 1 18 float and double are floating binary point types. In other words, they represent a number like this: 10001.10010110011 The binary number and the location of the binary point are both encoded within the value. decimal … Read more
How do I convert a string to a byte[] in .NET (C#) without manually specifying a specific encoding? I’m going to encrypt the string. I can encrypt it without converting, but I’d still like to know why encoding comes to play here. Also, why should encoding even be taken into consideration? Can’t I simply get … Read more
It is discouraged to simply catch System.Exception. Instead, only the “known” exceptions should be caught. Now, this sometimes leads to unnecessary repetitive code, for example: try { WebId = new Guid(queryString[“web”]); } catch (FormatException) { WebId = Guid.Empty; } catch (OverflowException) { WebId = Guid.Empty; } I wonder: Is there a way to catch both … Read more
I want to do something like: MyObject myObj = GetMyObj(); // Create and fill a new object MyObject newObj = myObj.Clone(); And then make changes to the new object that are not reflected in the original object. I don’t often need this functionality, so when it’s been necessary, I’ve resorted to creating a new object … Read more
What are the correct version numbers for C#? What came out when? Why can’t I find any answers about C# 3.5? This question is primarily to aid those who are searching for an answer using an incorrect version number, e.g. C# 3.5. The hope is that anyone failing to find an answer with the wrong … Read more
How can you enumerate an enum in C#? E.g. the following code does not compile: public enum Suit { Spades, Hearts, Clubs, Diamonds } public void EnumerateAllSuitsDemoMethod() { foreach (Suit suit in Suit) { DoSomething(suit); } } And it gives the following compile-time error: ‘Suit’ is a ‘type’ but is used like a ‘variable’ It … Read more
Example (note the case): string s = “Hello world!”; String s = “Hello world!”; What are the guidelines for the use of each? And what are the differences? 6 63
There are multiple threads(a, b, c etc.) about the fact that Clear() ing items in the .NET component containers does not Dispose them(by calling Dispose(true). Most frequently, IMHO, the Clear-ed components are not used anymore in the application, so it needs explicitly be Disposed after Clearing them from the parent containers. Maybe is a good … Read more