Browsing 15980 questions and answers with Jon Skeet

Moving bits to left

byte right =0xf; // 00001111 System.out.println(Integer.toBinaryString(right)); // 00001111 right = (byte)(right << 4); ...
Jon Skeet
people
quotationmark

If you hadn't cast to byte, all would be well. There are no shift operators defined for byte, so the value is first promoted to int - which is fine, and still 0000[...]1111. You then shift left 4 bits, giving 0000[...]111000. You then... more 11/17/2017 6:29:05 PM

people

c# Correct way to convert string value to decimal

I need convert the any value in format "101.46" to decimal. string s = "101.46"; decimal value = Convert.ToDecimal(s); But the decimal final value is always 10146. I need the...
Jon Skeet
people
quotationmark

Convert.ToDecimal will use the currently executing thread's culture - and my guess is that in your case, that's a culture that uses , as the decimal separator rather than .. You just need to specify the right culture - which is often the... more 11/17/2017 3:35:19 PM

people

Fetching descendant nodes always return null XML C#

I have an XML file that looks like this <modules> <level4> <module> <name>Programming Principles I</name> <credit>20</credit> ...
Jon Skeet
people
quotationmark

Modifying a lazily-evaluated query while iterating over it is basically dangerous. Two options: Use the Remove extension method. Less code, and it'll work! Replace your whole loop with: assessment.Remove(); Evaluate the query before... more 11/17/2017 3:27:11 PM

people

How to convert utf8 byte array to a string of given length

Let's say I have an array of bytes: var myArr = new byte[] { 0x61, 0x62, 0xc4, 0x85, 0xc4, 0x87 }; So it has 6 elements while it corresponds to utf8 abąć which has 4 letters....
Jon Skeet
people
quotationmark

It looks like Decoder has your back here, in particular with the somewhat huge Convert method. I think you'd want: var decoder = Encoding.UTF8.GetDecoder(); var chars = new char[4]; decoder.Convert(bytes, 0, bytes.Length, chars, 0,... more 11/17/2017 2:19:02 PM

people

Is there a better way to write lots of repeated instances of "if(string.IsNullOrEmpty())"

Sorry if this is something that has been answered before, I wasn't really sure what to search for. I have some code which has lots of null/empty checks before executing single...
Jon Skeet
people
quotationmark

In this case, you can use the fact that LINQ to XML ignores null values on construction. Create a small local method that takes an element name and the value, and returns either an element or null: private XElement CreateWhereElement() { ... more 11/17/2017 1:57:41 PM

people

Getting NodaTime.Serialization.JsonNet to work with a custom date format

I'm trying to deserialize a JSON, which contains dates in the format 2017-10-26 13:32:11 Etc/GMT. NodaTime seems to support this when combined with Json.NET and...
Jon Skeet
people
quotationmark

Currently, you're creating a specific ZonedDateTime pattern - but you're not actually telling Json.NET about that pattern anywhere. If this is the only Noda Time type that you need to use, I'd advise that you don't call... more 11/17/2017 8:46:01 AM

people

Current date time in specific timezone with and without DST offset with NodaTime

I need to sync a device internal clock on a device according to which timezone the users has selected. I'm using the following code to get the current time for that zone with...
Jon Skeet
people
quotationmark

It sounds like you're nearly there. You presumably want the OffsetDateTime or LocalDateTime, which you'd get like this: var now = SystemClock.Instance.GetCurrentInstant(); var zoneInterval = zone.GetZoneInterval(now); var offset =... more 11/16/2017 8:29:56 PM

people

Where is c# 7.2 in visual studio project settings?

Ive seen people using and discussing c# 7.2 features but I cant seem to find it. Ive got latest updates and only up to version 7.1 is listed. why and how can I get...
Jon Skeet
people
quotationmark

Version 15.4.* of VS 2017 doesn't support C# 7.2. C# 7.2 support is introduced in VS 2017 version 15.5 (currently in preview). more 11/16/2017 11:36:38 AM

people

Implementing the IComparable Interface

I'm reading the book c#6.0 in a Nutshell now, the code below is on topic "Implementing the IComparable Interfaces". I don't get a few things: Why the IComparable.CompareTo is...
Jon Skeet
people
quotationmark

You could implement IComparable implicitly, yes. But fundamentally you want to try to discourage users from comparing a Note with anything other than another Note. You may have legacy usages if IComparable, but if anything knows about the... more 11/16/2017 10:23:55 AM

people

What happens if Var args method also exists with overloaded methods exists in a Java method

Here is a program with method overloading done. package com.tests; public class BasicClass { public void getMe(Object a) { System.out.println("getMe 1"); } ...
Jon Skeet
people
quotationmark

This is often done for optimization. It allows common operations (providing up to four values) to be performed without creating an array, whilst still giving the flexibility of allowing an arbitrary number of arguments. To give a... more 11/15/2017 9:26:46 AM

people