Browsing 7239 questions and answers with Jon Skeet

Why does using a ConditionalAccessExpression change how my extension method works?

I have an extension method that looks like this: public static bool DoesNotExist(this object toCheck) { return toCheck == null; } generally I use it like...
Jon Skeet
people
quotationmark

A null conditional expression always has a nullable return type - after all, it has to have an overall result of null if the left hand side is null. So the type of myObject?.MyProperty.DoesNotExist() is Nullable<bool>, which can't... more 12/5/2016 9:50:09 AM

people

Shift all character codes inside a String by a constant value

By use of Java. This code works: for (char c : sourceString.toCharArray()) { destString += (char) (c + shiftValue); } System.out.println(destString); Is there a...
Jon Skeet
people
quotationmark

Well I'd avoid using repeated string concatenation, to start with. That's a very well known performance problem. In this case, you know the exact length you need to start with, so you don't even need a StringBuilder - a char[] is... more 12/5/2016 7:22:17 AM

people

C# Using .Min() on a Dictionary, min value, but return a key?

I'm not sure if this is even possible, as far as I can tell, it isn't, but I have a dictionary and I want to find the Min() of values, but return either the KVP or Key. Currently...
Jon Skeet
people
quotationmark

You can use MoreLinq and its MinBy method: var pair = dictionary.MinBy(x => x.Value); var key = pair.Key; (And yes, this is O(N) rather than O(N log N) - more efficient than ordering. It won't need as much memory, either.) more 12/2/2016 7:11:16 PM

people

Comparison of C# Linq syntax vs VB Linq syntax

I am trying to move some of my C# code into VB. When I run the C# code thru Telerik's translator it spits out the VB code below. The VB code will not compile and gives and error...
Jon Skeet
people
quotationmark

The problem is that you're using Function, which is for things which return a value. You should be able to use: ForEach(Sub(a) a.Remove()) more 12/2/2016 4:34:27 PM

people

Restrict generic extension method from extending strings

I have a very generic extension method to show any type of list within a console: public static void ShowList<T>(this IEnumerable<T> Values) { foreach (T item in...
Jon Skeet
people
quotationmark

This feels like a bit of an odd requirement to start with, to be honest - if something should work for any sequence of characters, then it should work for a string, which is a sequence of characters. If you really want to make it fail to... more 12/2/2016 9:52:12 AM

people

C# Parse list of git commits using json.net

I've been browsing various websites and the JSON.net docs but I can't find an elegant way to do this. The Problem I have to parse a list of GitHub commits since a certain...
Jon Skeet
people
quotationmark

You can just use LINQ to JSON very easily in this case - parse the text as a JArray, then ask for the sha property of each object: using System; using System.Collections.Generic; using System.IO; using System.Linq; using... more 11/30/2016 7:23:47 AM

people

java PreparedStatement syntax issue

I have the following code public void savePosition(String positionName) { String sqlStatement = "INSERT INTO positions (name) VALUES (?)"; try ( Connection...
Jon Skeet
people
quotationmark

You've put the entire body of your try statement into the part that's meant to only be for initializing closeable resources. You want: // This part is initializing resources try (Connection connection = getConnection(); ... more 11/29/2016 11:26:00 AM

people

How can i use the select method for Multiple properties with same property name

Controller public JsonResult GetFee(int? Class_Id) { var list = Repository.FeeRepository.GetAll() .Where(i => i.Class_Id == Class_Id) .Select(x => new...
Jon Skeet
people
quotationmark

You need to specify the names you want in the anonymous object creation expression, e.g. .Select(x => new { SchoolName = x.tbl_SchoolProfile.Name, ClassName = x.tbl_Classes.Name, FeeHeadName = x.tbl_FeeHead.Name, ... more 11/29/2016 11:13:22 AM

people

How to know date at a timezone if date at UTC in known and time at that timezone is known?

For example: I know the date in UTC is 2020-05-15 I know the Time in Pacific/Auckland (+12) is 8:00 How do I calculate the date in Pacific/Auckland (+12) with this known...
Jon Skeet
people
quotationmark

I'd take the simple approach of "try the obvious date/time combination, and see whether it works": Try a DateTime using the "UTC date" and local time, in the given time zone. In this case, you'd end up with 2020-05-15T08:00:00 in... more 11/28/2016 4:40:06 PM

people

Cannot implicitly cast enum to integer in C#

I can define an enum with integer as value: public enum FieldType { Bar = 0, Foo = 1 } Why C# doesn't allow creating a dictionary with integer as a key and using an...
Jon Skeet
people
quotationmark

If you could do that, you'd lose half the point of using enums at all - the point is to keep them as separate types to avoid you making mistakes involving using types incorrectly. It seems to me like your dictionary should actually have... more 11/28/2016 8:11:15 AM

people