Browsing 7239 questions and answers with Jon Skeet

Field and Property pair shortening in C#

I have a couple of these properties scattered about my code: private Animator _anim; public Animator anim { get { if (_anim == null) { _anim...
Jon Skeet
people
quotationmark

Basically, no - there's nothing to let you use automatically implemented properties with "just a bit" of custom code. You could shorten your code, though to: public Animator Animator { get { return _anim ?? (_anim =... more 8/23/2015 7:07:25 PM

people

How to find amount of hours in a day in NodaTime?

I'm writing a test app to reproduce some data writing scenario - a task that runs each hour. For that purpose I need to know how many times it will execute - how many hours there...
Jon Skeet
people
quotationmark

Between two local date/time values, there will always be 24 hours. It sounds like you're interested in a date in a particular zone, in which case you want: var startOfDay = zone.AtStartOfDay(date); var startOfNextDay =... more 8/22/2015 8:29:41 AM

people

How do I get the list of strings in tzdb used as time zone initializer?

SO I am new to NodaTime and trying to use it to for storing timezone information using DateTimeZone object. I came across below sample in user guide etc. which give me a nice...
Jon Skeet
people
quotationmark

To fetch the time zone IDs programmatically, use the Ids property in IDateTimeZoneProvider. For example, to find all zones: var provider = DateTimeZoneProviders.Tzdb; foreach (var id in provider.Ids) { var zone = provider[id]; //... more 8/22/2015 8:23:23 AM

people

C# Get Integer Byte Array in String

I have a random integer value which I need to represent in String as a Byte array. For example: int value = 32; String strValue =...
Jon Skeet
people
quotationmark

Don't take this approach at all. You should be writing to a binary stream of some description - and write the binary data for the length of the packet/message, followed by the message itself. For example: BinaryWriter writer = new... more 8/19/2015 6:48:16 PM

people

Why is the base type of an open generic type not open?

Consider a piece of the code below: public class A<T> { } public class B<T> : A<T> { } In such case: var a =...
Jon Skeet
people
quotationmark

So it is closed using a non-existing type of name "T" and only doing GetGenericTypeArgument on it makes it open again. Why is that? Because there is one type argument provided - the type parameter to B. Look at how you're specifying... more 8/19/2015 9:27:15 AM

people

asp.net get date from yyMMdd

I have this small code here: @foreach (var date in ViewBag.MissingDays) { var isoDate = date.ToString("yyMMdd"); <div class="col-md-1"> <input...
Jon Skeet
people
quotationmark

You probably want to keep the value part as it is - or ideally as yyyyMMdd, as two-digit year formats are horrible - and just format the display part differently. I don't think there's anything to use two-letter month abbreviations - the... more 8/18/2015 8:29:51 AM

people

in case of TreeMap , if pass our own class object as key then which interface is needed to be implemented Comparable or Comparator and why?

In case of "TreeMap" , if pass our own class object as key then which interface is needed to be implemented Comparable or Comparator and why?
Jon Skeet
people
quotationmark

If you construct the TreeMap specifying a comparator, then that will be used to compare the keys. If you construct the TreeMap without specifying a comparator, then the keys must implement Comparable. Typically the key would implement... more 8/15/2015 12:02:34 PM

people

Similar Extension methods are there any differences?

What are the differences between these two extension methods? public static class Test { public static int First<T>(this T obj) { return 2; } ...
Jon Skeet
people
quotationmark

There are a few differences, yes. The first one won't box value types - but will end up JIT-compiling the method multiple times for different type arguments (once for all reference types, and once per value type). So: byte x1 = 10; int... more 8/15/2015 10:28:13 AM

people

Access to modified closure: ReSharper

I created a library that handles database access. I recently added transaction handling; however, I came across a small issue. To outline this, I wrote this sample for...
Jon Skeet
people
quotationmark

I would suggest avoid using a ref parameter for this in the first place - it seems needlessly complicated to me. I'd rewrite DoAction as: static string DoAction(string data, Action<string> action) { data = data == null ?... more 8/15/2015 10:20:07 AM

people

Does 'await Task.Delay(1000)' block ANY thread?

I've read that Task.Delay() is advised to be awaited not to block the calling thread (as oposed to Task.Delay().Wait() and Thread.Sleep()). But as I learned more about async/await...
Jon Skeet
people
quotationmark

however it blocks SOME thread, where the awaited task gets shifted to? It depends on what you mean by "block". It doesn't cause any thread to go to sleep for 1 second, or (worse) spin wait for the delay to complete. Instead, it... more 8/15/2015 10:15:40 AM

people