Browsing 15980 questions and answers with Jon Skeet

NodaPatternConverter for Instant with numeric (unix) format in 2.x.x

As I can read on https://nodatime.org/2.0.x/userguide/migration-to-2 the support for numeric formatting of Instants has been removed. Is there currently a way to create a...
Jon Skeet
people
quotationmark

Well, you can implement IPattern<T> yourself. Your parser would just need to use long.Parse then call Instant.FromUnixTicks. The formatter would just need to call Instant.ToUnixTimeTicks and format the result. Ideally, do both of... more 10/24/2017 6:51:25 AM

people

Why string.Format does not throw ArgumentNullException?

According to MSDN String.Format throws if format is null (pretty reasonable), link here. But testing says it only does that if the second argument is null as well, not if the...
Jon Skeet
people
quotationmark

Ah, the joys of overload resolution. In that case, you're actually calling string.Format(IFormatProvider, string, params object[]) - so you're passing a null argument for the provider parameter, which is entirely valid (and means to use... more 10/23/2017 4:39:04 PM

people

C# ToDictionary get Anonymous Type value before C # 7

Hello here is how i get value from dictionary myage value after C# 7 static void Main(string[] args) { List<User> userlist = new List<User>(); ...
Jon Skeet
people
quotationmark

Three options: First, you could write an extension method like this: public static TValue GetValueOrDefault<TKey, TValue>( this IDictionary<TKey, TValue> dictionary, TKey key) { TValue value; ... more 10/23/2017 2:44:58 PM

people

Difference between 1/0 and 1.0/0.0 in java

I am new in Java programming language, a stupid question comes up as below. to execute 1/0 in java will be generated a runtime exception, as the value is undefined. but the...
Jon Skeet
people
quotationmark

anything special with double data type? Yes, double can represent infinity (as can float), whereas int can't. The floating point types follow IEEE 754, which allows for signed infinity values, as well as "not a number" (NaN) values,... more 10/23/2017 9:50:51 AM

people

Arrays first index displaying wrong element

I am currently studying C# language and getting 0 on the first index[0] where I am looking to see the user input which is "yx". Here's what I have written: using...
Jon Skeet
people
quotationmark

This loop doesn't do what the code suggests you think it does: foreach (var newemptyarray in y) That's iterating over the elements in y, which are integers. So newemptyarray isn't an array, it's just a value. On the first iteration of... more 10/23/2017 6:47:09 AM

people

How is switch variable declaration scoped?

How is following possible? switch (param.ParameterType) { case Type x when x == typeof(byte): int invalid; break; case Type x when x == typeof(short): ...
Jon Skeet
people
quotationmark

Question is, how is x scoped inside each case without any visible blocks. meanwhile, variable invalid cant be declared in different switch cases. it has to be inside a block. Variables introduced via pattern matching in case labels... more 10/22/2017 5:56:52 PM

people

Use of await keyword in c#

I have a doubt. I have read that await keyword is used when we want to wait for a particular function to finish the operation. public async void Work() { await...
Jon Skeet
people
quotationmark

But I have a doubt that even if the await and async keyword are not used then also Work() method is going to wait for SlowTask() method to complete its execution before printing "Execution completed" because it will be executed line by... more 10/22/2017 8:55:43 AM

people

How to mark a C# assembly as a CLS Compliant when using new csproj format?

I recently upgraded a C# project from xproj format to the new csproj. During the migration, the old AssemblyInfo.cs has been removed so I'm wondering how I can indicate that my...
Jon Skeet
people
quotationmark

Just add AssemblyInfo.cs back, with only the parts that aren't autogenerated. There's nothing to stop you from applying assembly attributes in code - it's just that most of the common attributes are provided from the project settings. more 10/21/2017 8:14:23 PM

people

Different property value for contracts

I have two interfaces implemented by one main class. How can i refactor my code in a way that on implementing each contract, the methods of each contract has a different value for...
Jon Skeet
people
quotationmark

It sounds like all you need is explicit interface implementation: public class Class1 : Interface1, Interface2 { // Note the lack of access modifier here. That's important! Data Interface1.GetData() { // Implementation... more 10/20/2017 6:29:56 AM

people

Why can't the type conversion of `this` be implicitly inferred from the generic contraint?

I have the following class: public class Item<TItem> where TItem : Item<TItem> { void GetReference() { TItem item = this; } } Here TItem...
Jon Skeet
people
quotationmark

Because it wouldn't be safe. Consider: public class GoodItem : Item<GoodItem> { // No problem } public class EvilItem : Item<GoodItem> { // GetReference body would be equivalent to // GoodItem item = this; //... more 10/19/2017 4:59:16 PM

people