Browsing 7239 questions and answers with Jon Skeet

Read spreadsheet without knowing its range

I would like to read a spreadsheet without knowing its range. Is that possible in C#? Or else I have to specify the range till the values are inserted in the sheet? These things...
Jon Skeet
people
quotationmark

The documentation for A1 notation (which is what the range is expressed in) includes: Sheet1 refers to all the cells in Sheet1. So if you really want all the cells, just use that, without a trailing !. If you want to find out the... more 11/21/2017 7:32:03 AM

people

How are primitive data types made in C#?

How do System.float, System.int and other primitives types work? I never understood how it was possible to make primitives structs and I wonder if I could make my own numeric...
Jon Skeet
people
quotationmark

Assuming we're talking about a C# compiler which targets the Common Language Infrastructure (CLI), as almost everything does, it basically uses the primitive types exposed by the CLI. There are effectively three levels of support to... more 11/20/2017 8:12:56 AM

people

In which version of C# Indexers were introduced?

In which version of C# or .Net Framework Indexers were introduced ?
Jon Skeet
people
quotationmark

They've always been in C# and .NET, right from version 1.0. more 11/18/2017 8:52:02 AM

people

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