Browsing 7239 questions and answers with Jon Skeet

Func optional output parameters? C#

i'm trying to create a flexible insertion sort algorithm which will sort record object properties (months, days and times, mostly numeric data) so I'm passing Func delegate as a...
Jon Skeet
people
quotationmark

It sounds like you just need to make it generic, and constrain the type to have a comparison: public static void InsertionSort<T>( RecordCollection records, Func<SesmicRecord, T> propertySelector) where T :... more 4/11/2017 3:04:09 PM

people

Why does LastName not show in C# string concat operator below

I've got code like the following where adding the ?? operator causes last to disappear. Expected Result: was that if first was null then first would be replaced by "" and...
Jon Skeet
people
quotationmark

It's a matter of precedence. + binds more tightly than ??, so your code is effectively: var str2 = first ?? ("" + last) It sounds like you probably meant: var str2 = (first ?? "") + last But there's no point, given that string... more 4/11/2017 2:59:17 PM

people

Reflection: How do I find and invoke a local functon in C# 7.0?

I have a private static generic method I want to call using reflection, but really I want to 'bundle' it inside of another method. C# 7.0 supports local functions so this is...
Jon Skeet
people
quotationmark

Okay, I've got a solution. But it's really horrible. It involves creating a delegate from your method with a specific type, then using that to find the generic method, then constructing another specific method and invoking it. So we go... more 4/11/2017 2:40:54 PM

people

Does static modifier change the access level of a class member in java?

I am reading the book of OCA & OCP for java 7 certification and I am trying the exercises of the book with java 8 and I noticed something wired. I have Class1 class as...
Jon Skeet
people
quotationmark

Everything is fine - that's how protected access is meant to work. It's specified in JLS 6.6.2.1: Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C. In... more 4/11/2017 2:05:24 PM

people

C# Compare one List string with substring of other list string

I have two lists List<string> ingoreEducationKeywords= new List<string>(){"Uni", "School", "College",}; List<string> userEducation= new...
Jon Skeet
people
quotationmark

It's a matter of phrasing what you want in a way that leads to a natural translation into LINQ: You want items from userEducation (that suggests you'll start with userEducation) Where none of ignoreEducationKeywords are... more 4/11/2017 12:02:45 PM

people

LINQ to XML pairing up two nodes inside XML document to a list or dictionary

I have a XML document that basically looks like this: <Item> <ItemID>1234567890</ItemID> <ItemSpecifics> <NameValueList> ...
Jon Skeet
people
quotationmark

You need to start with Item as that's the root of each entry that you want in the end. You also need to provide the namespace of the elements, which is defaulted in the root element of the doc. So something like: XNamespace ns =... more 4/11/2017 10:17:26 AM

people

Custom IComparer for Int64

I have a about 5000 Int64 in a sorted List. I want to do a List.BinarySearch but only based on the 39 bits on the left. I am packing information in the bits to the right of...
Jon Skeet
people
quotationmark

Your comparer just needs to compare by masking and then comparing the results. (Shifting works too - they're basically equivalent here, although masking allows your key to be any set of bits in the input, rather than necessarily the most... more 4/10/2017 4:06:40 PM

people

MemoryStream output to a file c#

I have below code: string fileName = GetFileName(); using (var stream = new MemoryStream()) { using (var sw = new StreamWriter(stream)) { for (int i =...
Jon Skeet
people
quotationmark

My problem is in the else body Well you're handling that in a different way to in the first if body. In the first if body, you're explicitly flushing the writer and rewinding the stream: sw.Flush(); stream.Position = 0; You're not... more 4/10/2017 2:46:17 PM

people

C# constructor initializing properties instead of fields?

Let's say that you have to use an constructor to initialize some fields... class Foo { private int price; public Foo(int price) { this.price = price; ...
Jon Skeet
people
quotationmark

It's fine for a constructor to initialize properties, but it's rarely useful to have private automatically-implemented properties. I'd always use properties rather than fields for non-private state (whether those properties are... more 4/9/2017 4:49:34 PM

people

property overriding not working in C#?

Here i have written Special Customer class which is child class and i have overridden Property Cname where i want Special Customer can change the Name only if they Cname="Special"...
Jon Skeet
people
quotationmark

Your code is working already - it's calling the SpecialCustomer.Cname setter, as you can easily tell either by setting a breakpoint in it or adding some logging. (I just added a Console.WriteLine statement in the setter.) However, it's... more 4/9/2017 6:44:30 AM

people