Browsing 15485 questions and answers with Jon Skeet

Compute Week of Year issues with ZonedDateTime and Calendar API

I was trying to compute week of year from a ISO-8601 Date format String input. Initially I tried this with java.time.ZonedDateTime but it gives incorrect result for Input Date -...
Jon Skeet
people
quotationmark

There are four problems with your code to start with: You're using the system default time zone when you use Calendar, which may well change which date the Instant falls on. If you set the calendar to use UTC you'll make it more... more 1/16/2017 9:58:56 AM

people

Why does my async method not wait on "serialPort = await SerialDevice.FromIdAsync()"?

I'm trying to make a class that initializes a serial connection between a raspberry and arduino. The class should also be capable of reading and writing over the established...
Jon Skeet
people
quotationmark

All your async methods return void - that means they start executing, and the calling code has no easy way of waiting until it's actually completed before continuing. As soon as your use an await expression on something that hasn't... more 1/15/2017 6:33:24 PM

people

Convert DateTime with DateTimeKind.Unspecified from utc to local

I have a database where all dates are UTC, and the driver always sends me DateTime objects with DateTimeKind.Unspecified. I want to assume they are in UTC and convert to local...
Jon Skeet
people
quotationmark

If you're definitely using Noda Time and want a ZonedDateTime, you could "tell" Noda Time that you've really got a UTC value, then convert it: var zoned = LocalDateTime.FromDateTime(utcDate) ... more 1/15/2017 2:40:50 PM

people

Casting object instance to actual type

I send several different kinds of package classes, which I serialize and send over the network. All packages extend the AbstractPackage: public abstract class AbstactPackage()...
Jon Skeet
people
quotationmark

You could use a dictionary from the type to a delegate to call when you receive a package of that type. So for example: class PackageReader { private static readonly Dictionary<Type, Action<AbstractPackage>>... more 1/14/2017 1:17:13 AM

people

BCL and FCL both are NameSpaces?

I need a clarity regarding class libraries. Base Class Libraries and Framework Class Libraries both are comes under namespaces-I mean can we call both are namespaces? if not which...
Jon Skeet
people
quotationmark

No, they're not namespaces. A namespace is something like System, System.IO or System.Collections.Generics. The libraries contain types that are in namespaces, but the libraries aren't namespaces themselves. more 1/13/2017 12:33:32 PM

people

When is assignment to a ref argument effective in C#?

Suppose a method is changing the value of an argument passed by reference. Is the effect of such operation immediately visible across the application or only after the method...
Jon Skeet
people
quotationmark

Is the effect of such operation immediately visible across the application or only after the method returns? It's immediately visible - because basically, what you end up passing is the variable itself, not the variable's value.... more 1/11/2017 4:42:04 PM

people

What effect does including async in signature (without await in body) have on program flow?

General question I've just tracked down a strange behaviour to the the absence of an async modifier in the signature of a method. With it, I get the expected behaviour, but...
Jon Skeet
people
quotationmark

My question is: what is async really doing when there isn't an await? In terms of observable behaviour: The code will still run synchronously, as the warning says The result (including any exception thrown) will be wrapped up in a... more 1/11/2017 2:44:56 PM

people

One method accepting different parameters

Using FW 4.5.1 I have a method: void DoSomething(string s) I want to allow the function to accept int as well, but under the same parameter s. That means I want to write: void...
Jon Skeet
people
quotationmark

Three options for you to consider, depending on what you're actually trying to achieve: Use overloads public void DoSomething(int s) { ... } public void DoSomething(string s) { ... } This will only accept int and string - which could... more 1/10/2017 2:17:07 PM

people

XNamespace as additional attribute, why

I want to achieve this: <?xml version="1.0" encoding="utf-8"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> ...
Jon Skeet
people
quotationmark

You're confusing a namespace alias (which you want to be xdt) with the namespace URI. You want to put the elements in the right namespace (by URI) but specify an xmlns attribute in the root element with the alias you want: using... more 1/9/2017 10:16:44 PM

people

List<T> Concatenation dynamically

I am trying to concate List<> as follows- List<Student> finalList = new List<Student>(); var sortedDict = dictOfList.OrderBy(k => k.Key); foreach...
Jon Skeet
people
quotationmark

Other answers have explained why Concat isn't helping you - but they've all kept your original loop. There's no need for that - LINQ has you covered: List<Student> finalList = dictOfList.OrderBy(k => k.Key) ... more 1/9/2017 8:50:36 AM

people