Browsing 7239 questions and answers with Jon Skeet

call sum in expression tree

I have this query: Dim test = result.GroupBy(Function(row) groupedindexes.Select( Function(grpindex) row(grpindex)).ToArray, comp). ...
Jon Skeet
people
quotationmark

Firstly, you're looking in Enumerable for methods - that's not a good idea if you're trying to work with expression trees. You should be looking in Queryable. Next, you need to understand that the Sum method is overloaded - and there are... more 1/29/2015 8:41:45 PM

people

Confused about C# character special meanings

I'm a newbie in C# programming, and I don't understand how character escaping works. I tried many of them, but only '\t' works like it should. An example for my code: ...
Jon Skeet
people
quotationmark

You need "\r\n" for a line break in Windows controls, because that's the normal line break combination for Windows. (Line breaks are the bane of programmers everywhere. Different operating systems have different character sequences that... more 1/29/2015 7:29:43 PM

people

Create an interface with a method that's 'generic', but it's generic type is the implementer of the interface

Is there any way in C# to have a generic type that is always the implementing type in an interface? Something like this: interface Foo { this GetOtherThis(); } class Bar :...
Jon Skeet
people
quotationmark

Is there any way in C# to have a generic type that is always the implementing type in an interface? No. The answers given so far don't satisfy this, for two reasons: You can always implement an interface with a different... more 1/29/2015 7:17:37 PM

people

how to retrieve keys only when filter by value

ConcurrentDictionary<int, int> dic = new ConcurrentDictionary<int, int>(); dic.AddOrUpdate(1, 2, (s, i) => 0); dic.AddOrUpdate(2, 3, (s, i) =>...
Jon Skeet
people
quotationmark

Just select the entries, filter based on the values, then project to the keys: var keys = dic.Where(entry => entry.Value > 5) .Select(entry => entry.Key); Note that this approach is fine for any... more 1/29/2015 6:46:27 PM

people

Noda Time Unit Testing XML Error

I am trying to setup a simple unit test using Noda Time. My code is: public void ValidityPassedDate() { FakeClock fakeClock = new FakeClock(SystemClock.Instance.Now); ...
Jon Skeet
people
quotationmark

You need to add a reference to the System.Xml assembly (not a using directive for the System.Xml namespace). Do that in Solution Explorer - it's not a code issue. This is unfortunate, but as far as I can tell it's an unavoidable... more 1/29/2015 5:47:04 PM

people

Java is not recognizing an object created as an array

I am creating a simple mechanical computer emulator, with virtual counters and punch cards, but I keep getting errors in java. It creates counters as an array of objects like...
Jon Skeet
people
quotationmark

You've got at least four problems: You're declaring the counter variable as a local variable inside your method. I suspect you meant to declare a field somewhere - or return a reference from the method, and assign it to an instance... more 1/29/2015 3:45:41 PM

people

Noda Time Date Comparison

I am new to Noda Time and I basically want to compare if a date has expired or not. In my case I have an object with the date it was created, represented by a LocalDate and the...
Jon Skeet
people
quotationmark

To get the current date, you need to specify which time zone you're in. So given a clock and a time zone, you'd use: LocalDate today = clock.Now.InZone(zone).Date; While you can use SystemClock.Instance, it's generally better to inject... more 1/29/2015 3:31:14 PM

people

How to send an array of ints to a function that accepts array of IComparable?

I have a C# function that accepts an array of IComparable public static void sort(IComparable[] a){//...} If I send an array of strings to this function it is accepted, but...
Jon Skeet
people
quotationmark

Although an int is an IComparable, an int[] isn't an IComparable[]. Imagine if it were: int[] x = new int[10]; IComparable[] y = x; y[0] = "hello"; That would be trying to store a reference in an int[]. Badness. Basically, value-type... more 1/29/2015 3:21:42 PM

people

Java MD5 encoder not match with C# MD5CryptoServiceProvider

I am trying to generate C# MD5CryptoServiceProvider to encode string and Java MessageDigest.getInstance("MD5") to encode string, but both outputs are different. There are so many...
Jon Skeet
people
quotationmark

Neither your C# code nor your Java code are good ways to convert a hash to a string. I strongly suspect you've got the same bytes in both cases, but you're converting them to strings differently. Your C# code is just converting each byte... more 1/29/2015 1:59:33 PM

people

Error while writing Date to Image MetaData in C# String was not recognized as a valid DateTime

Im trying to write metadata to images using the following code.But when i try to add the date field an error is thrown String was not recognized as a valid DateTime using (Stream...
Jon Skeet
people
quotationmark

Based on the exception and the example in the documentation, I suspect you need to format this as a US short date. The simplest way of doing this is probably just to use the invariant culture. For example: metadata.DateTaken =... more 1/29/2015 1:47:50 PM

people