Browsing 7239 questions and answers with Jon Skeet

Indexer explicit implemented IFormattable

I wrote myself an indexer, it works but as soon as I implement "IFormatable" explicit, it does not work anymore. Why is does it not work and how to use it explicit ? This is...
Jon Skeet
people
quotationmark

By implementing ToString(string format, IFormatProvider formatProvider) explicitly, you can only call it via a reference with a compile-time type of IFormattable. You can do that by using this: return... more 8/7/2015 10:00:23 AM

people

What is an alternative for Task.FromResult<T>() for tasks that represent operations returning void

What is the best way to return a task that doesn't have a generic type parameter? In other words a task that represents an operation that doesn't return anything or returns...
Jon Skeet
people
quotationmark

Task<T> extends Task - so it's reasonably common to just use Task.FromResult<object> and provide an empty result. For example: Task ret = Task.FromResult<object>(null); (Or use a value type - it really doesn't matter... more 8/7/2015 9:40:50 AM

people

Get original value from HashSet

I have a problem with HashSet because it does not provide any method similar to TryGetValue known from Dictionary. And I need such method -- passing element to find in the set,...
Jon Skeet
people
quotationmark

I agree this is something which is basically missing. While it's only useful in rare cases, I think they're significant rare cases - most notable, key canonicalization. I can only think of one suggestion at the moment, and it's truly... more 8/7/2015 7:48:37 AM

people

Which is the right option? C or D?

public class Sequence { Sequence() { System.out.print("c "); } { System.out.print("y "); } public static void main(String[] args) { ...
Jon Skeet
people
quotationmark

Basically, it's just a matter of reading through JLS 12.5 (Creation of New Class Instances) carefully. In particular, note the order of: Execute the instance initializers and instance variable initializers for this class [...] ... more 8/7/2015 6:22:32 AM

people

LINQ query syntax for selectmany + join + let

I want to compare a list of named data dictionaries with actual data read from a data provider. The result should be a flat list in the form: [Table]: [Key] changed from...
Jon Skeet
people
quotationmark

The point of a join is to find matching elements in two independent data sources. In other words, the elements in the right hand side of the join can't depend on the element on the "current" left hand side of the join. So you can't join... more 8/7/2015 6:04:04 AM

people

Why this SqlParameterCollection call is throwing error?

This is code sample to call a stored procedure. Second process throws InvalidCastException at runtime. I am trying to add a new SqlParameter into the SqlParametersCollection. I...
Jon Skeet
people
quotationmark

I suspect you haven't seen that exact syntax. Look at what you've got: db.Parameters.Add(new SqlParameter("@pID", SqlDbType.Int).Value = 12345); That's calling db.Parameters.Add with an int argument. I suspect what you've actually seen... more 8/6/2015 6:59:44 PM

people

Does a map using equals method for key checking exists?

I want to store data in a map, with key unicity, but I would like the map to use the equals method of my key class. It seems that HashMap doesn't use the equals method (I may be...
Jon Skeet
people
quotationmark

It seems that HashMap doesn't use the equals method (I may be wrong, if so my tests are wrong). It does use equals, but it uses hashCode first. It will only bother calling equals on keys with the same hash code - that's how it manages... more 8/6/2015 4:05:24 PM

people

How to compare two generic "Type" variables?

I'm confused... I have no idea why this particular problem is occurring and I was wondering if someone could shed some light on the subject. I'm doing some reflection operations...
Jon Skeet
people
quotationmark

I suspect you're running into a problem due to the T in ICollection<T> being different in each case. Here's a short but complete example: using System; interface IFoo<T> {} public class Foo1<T> : IFoo<T>... more 8/6/2015 2:26:58 PM

people

Auto property with initializer referencing instance member

This is working code public class SomeViewModel { public DelegateCommand CommandA { get; } public DelegateCommand CommandB { get; } public bool SomeProperty { get;...
Jon Skeet
people
quotationmark

There's nothing new here, really. Forget automatically implemented properties - you simply can't refer to this within a field initializer. (Let's face it, you're trying to initialize a field under the hood - just a field which is only... more 8/6/2015 12:39:31 PM

people

SelectSingleNode in XML for select second element doesn't work in C#

I have problem to select second node from root element in following example xml code: <?xml version="1.0"?> <config> <FirstNode> ...
Jon Skeet
people
quotationmark

You're using // at the start of each of your selections - which means "find descendant nodes starting at the root" (so the context is irrelevant). You could either do things in one step as per Jeffrey's answer, or use relative... more 8/6/2015 12:24:51 PM

people