Browsing 7239 questions and answers with Jon Skeet

Invalid variance with delegate in interface method

Why does the following interface declaration produce an invalid variance error? public interface ICache<in TKey, TValue> { TValue GetOrAdd(TKey key, Func<TKey,...
Jon Skeet
people
quotationmark

TL;DR: The C# compiler is keeping you safe. Effectively, "nested input in an input position makes an output" when it comes to generic variance. Your method has a parameter which itself accepts a TKey, which sort of reverses the... more 8/29/2017 5:33:04 PM

people

Calling an async method without await in a non async method?

What is the behavior of calling an async method without the await and in a non-async method? I'm asking because I see Visual Studio not displaying any warning on the call to the...
Jon Skeet
people
quotationmark

The fact that a method has async as a modifier is an implementation detail of the method. It just means you can write asynchronous code more easily. It doesn't change how the method is called at all. Typically an async method returns Task... more 8/29/2017 3:47:36 PM

people

Parsing AM/PM time string to TimeSpan

I'm using the following to parse a time string to a TimeSpan: string[] formats = { "hhmm", "hmm", @"hh\:mm", @"h\:mm\:ss", @"h\:mm", "hh:mm tt" }; parseSuccess =...
Jon Skeet
people
quotationmark

tt doesn't exist as one of the format specifiers for custom TimeSpan format strings. That makes sense in that TimeSpan is really meant to be a duration, not a time-of-day value - it's unfortunate that DateTime.TimeOfDay does return a... more 8/29/2017 12:21:28 PM

people

Getting Wrong Date Using XMLGregorianCalender

I am trying to get the current date/time to populate in an XML. I am using the code XMLGregorianCalendar xmlDate = null; try { xmlDate =...
Jon Skeet
people
quotationmark

Look at the arguments you're passing in to newXMLGregorianCalendar: [...].newXMLGregorianCalendar(Calendar.YEAR, Calendar.MONTH+1, Calendar.DAY_OF_MONTH, Calendar.HOUR, Calendar.MINUTE, Calendar.SECOND, ... more 8/28/2017 4:29:40 PM

people

Passing "this" to base constructor

Is there any way to pass "this" to the base constructor? abstract class Base<TSelf> { public ICollection<TSelf> List; public Base(ICollection<TSelf>...
Jon Skeet
people
quotationmark

No, you can't use this in a constructor initializer. You'd have to add call Add(this) afterwards - but you can do that in the Base<TSelf> constructor, so long as you cast to TSelf. You need to cast this to object first before casting... more 8/27/2017 8:15:46 AM

people

SequenceEqual not calling Equals in parent type

A parent type: public class IdObject : IComparable<IdObject>, IEquatable<IdObject> { public int id { get; set; } public bool Equals(IdObject other) { ...
Jon Skeet
people
quotationmark

Basically, you've got a problem because your type doesn't override object.Equals(object) in a way consistent with your IEquatable<T> implementation and you're dealing with a collection of the subclasses. SequenceEqual will be using... more 8/25/2017 12:07:24 PM

people

Create library targeting .Net Framework 4.5 and .Net Standard

I have a .NET library (Products.SDK) which i need to make it compatible with both .NET Framework 4.5 and .NET Standard 1.4. (later on i will also create a NuGet package out of...
Jon Skeet
people
quotationmark

You use option two - but only use #if in places where you really need it. The example you gave had the same code in both branches. My Noda Time project takes exactly this approach, and we've had very few problems due to that. Where... more 8/22/2017 8:03:00 AM

people

why does params byte[] argument doesnt work?

i have this method from a pattern scanner public static int FindPFM(int Module,long ModuleL,int Offset,params byte[] pattern) { string mask =...
Jon Skeet
people
quotationmark

When you invoke a method with a parameter array, the arguments go at the end, not the beginning (to match the parameter array position). Next, the arguments don't go in parentheses as you've tried to do - that's the syntax for a C# 7 tuple... more 8/19/2017 8:48:36 PM

people

Dynamic Order By in Linq

I have an app that accesses database and has to order results by different fields depending on the input. Here is my function for sorting: IQueryable<Entity>...
Jon Skeet
people
quotationmark

Well, you definitely want to use OrderByDescending instead of reversing. It's not going to be quite as brief as the SQL, but you could at least use: IQueryable<Entity> GetSortedData(IQueryable<Entity> result, String orderby,... more 8/18/2017 4:44:41 PM

people

Generic functions and ref returns in C# 7.0

Is it possible to use the ref returns feature in C# 7.0 define a generic function that can do both comparison and update of a field in two instances of an Object? I am imagining...
Jon Skeet
people
quotationmark

You need to declare your own delegate type for it. For example: using System; public class Thing { public int field1; public string field2; } public delegate ref TOutput FuncRef<TInput, TOutput>(TInput input); public... more 8/18/2017 3:46:13 PM

people