Browsing 7239 questions and answers with Jon Skeet

Generic Compare throws "At least one object must implement IComparable."

My Fancy Field Updater throws. public static bool UpdateField<T>(ref T target, T value) { bool updated; if (0 !=...
Jon Skeet
people
quotationmark

It looks like you don't really want to compare the values in terms of "greater than or less than" - but for equality. So you should use EqualityComparer.Default: public static bool UpdateField<T>(ref T target, T value) { bool... more 7/7/2016 5:25:47 PM

people

Task.Wait() not waiting for task to finish

I have a console app and I want to launch tasks one after the other. Here is my code: static void Main() { string keywords = "Driving Schools,wedding services"; ...
Jon Skeet
people
quotationmark

Your async method just returns void, which means there's no simple way of anything waiting for it to complete. (You should almost always avoid using async void methods. They're really only available for the sake of subscribing to events.)... more 7/7/2016 9:29:06 AM

people

Simply IEnumerable<int> to List<KeyValuePair<int, int>>?

The following code works fine: cboDay.DataSource = Enumerable.Range(1, 31).ToDictionary(x => x, x => x).ToList<KeyValuePair<int, int>>(); cboDay.ValueMember =...
Jon Skeet
people
quotationmark

Sure, just use Select: cboDay.DataSource = Enumerable.Range(1, 31) .Select(x => new KeyValuePair<int, int>(x, x)) .ToList(); It's not clear whether you need this to be List<T>, but doing so guarantees that it's... more 7/7/2016 8:59:31 AM

people

VB.Net Encryption File İncrease byte array x7

i can ecnrypt files 100 mb or smaller but i transport file bytes to array memory usage increase x7 and program pass 2 gb memory limit Example Test.rar size 100 mb After...
Jon Skeet
people
quotationmark

Well yes, you're reading the whole file into memory and then encrypting the whole thing in one go. You don't need to do that - you can do the whole thing in a streaming approach. You need three streams: One stream to read input from One... more 7/7/2016 6:33:27 AM

people

Is .NET CLI only for .NET Core?

Do I use the .NET CLI if I want to create an ASP.NET Core 1.0 app that uses the .NET Framework? Is .NET CLI only for the new .NET Core library or both Core and .NET 4.6?
Jon Skeet
people
quotationmark

You can use dotnet cli for full framework apps and libraries as well. You just need to use the appropriate framework tag - for example "net46" to target .NET 4.6. You can target multiple frameworks, too: For example, from my Noda Time... more 7/6/2016 5:16:16 PM

people

Generic method with Ti and Ti+1

I have the following generic class using C# 6: public class IncludeMapper<T1> { private readonly Dictionary<String, List<LambdaExpression>> _properties = new...
Jon Skeet
people
quotationmark

Is there a way to simplify my code? Well, you could simplify it a bit: public class IncludeMapper<T1> { private readonly Dictionary<String, List<LambdaExpression>> _properties = new... more 7/6/2016 9:19:05 AM

people

Java Store and compare huge key value pair maps on disk

I have a scenario in which i have to compare 2 Maps. I have to take a keys of the source Map and iterate through the target Map for the key for equals and then compare their...
Jon Skeet
people
quotationmark

One reasonably simple option: Put the maps on disk Sort them (in any of a number of ways) Open a reader for each map, and read the first line Iterate over the lines of each map, deciding which map to read from based on a comparison of... more 7/6/2016 7:10:56 AM

people

C# Using ArrayList.IndexOf with multiple identical items

I've got a situation where I'm using an ArrayList to store a list of file names (full file path). When I add multiple items of the same file to the array, then use...
Jon Skeet
people
quotationmark

The simplest approach is just to use the index to iterate: for (int i = 0; i < list.Count; i++) { backgroundWorks.ReportProgress(i + 1); // Do stuff with list[i] } more 7/6/2016 6:04:29 AM

people

Limitation of generics in C#2

I'm currently reading Jon skeet's amazing book "C# in depth (3d version)". I'm stuck on p.99 dealing with lack of contravariance for generics. class TestGenericVariance { ...
Jon Skeet
people
quotationmark

The idea is that you make AreaComparer generic like this: public class AreaComparer<T> : IComparer<T> where T : IShape { public int Compare(T x, T y) { return x.Area.CompareTo(y.Area); } } Then when you... more 7/5/2016 3:50:25 PM

people

Create a single source project with full .net, PCL and dotnet core assemblies build from same C# source?

Update: This refers to an obsolete set of .net core tools built around project.json, this question and its answers are of very limited utility in current .net core toolsets like...
Jon Skeet
people
quotationmark

I would try to go for a pure project.json approach. For example, here's a project.json file which will build a single package which supports netstandard1.0, PCL profile 328, and .NET 4.6 - with each configuration defining its own symbols... more 7/5/2016 3:34:30 PM

people