Browsing 15485 questions and answers with Jon Skeet

Variant and open generics IReadOnlyList

I'm trying to understand why a specific behavior regarding variant and generics in c# does not compile. class Matrix<TLine> where TLine : ILine { TLine[] _lines; ...
Jon Skeet
people
quotationmark

You just need to add the class constraint to TLine: class Matrix<TLine> where TLine : class, ILine This will ensure that TLine is a reference type - which then allows generic variance to work. Variance only works for reference... more 1/6/2017 10:45:05 AM

people

LINQ Fetch items from list starting from specific index

With the help of LINQ, I need to fetch items from a list based on a condition. For that it should consider items only from (provided index - 3) to provided index...
Jon Skeet
people
quotationmark

It sounds like you just want a mix of Skip and Take, filtering with Where: var query = list.Skip(index - 3) // Start at appropriate index .Take(3) // Only consider the next three values ... more 1/6/2017 10:04:56 AM

people

Linq Lambda expression == and Equals operator

var list = new List<string>(); var word = "some word"; list.Add("some word"); list.Add("some sentence"); list.Add(word); I know Equals compare the value stored in the...
Jon Skeet
people
quotationmark

I know Equals compare the value stored in the reference while "==" compares whether two references point to the same object. No, that's just the behaviour of the default == operator for reference types. When both sides of == have a... more 1/5/2017 7:21:52 AM

people

Initialization of an array

I declare an array on dimension n, where n is the max number of elements that the array can contain: int array[] = new int [n]; I have an algorithm that fills this array with...
Jon Skeet
people
quotationmark

You can't, basically - not based on just the array itself. There is no difference between a value which was explicitly set to 0 and one which just defaulted to 0. You could change your array to be of type Integer[] - then you'd have null... more 1/4/2017 5:31:44 PM

people

C# overloaded constructors issue

I have the following class that I use to minimise code duplication when often calling different sets of data from an Oracle database. Primarily I need help to remove the code...
Jon Skeet
people
quotationmark

Normally I'd have a "canonical" constructor which all the other constructors chain to. In your case that would involve creating an empty list though: public UniformData(string sql) : this(sql, new List<SqlParameters>()) { } public... more 1/4/2017 3:12:25 PM

people

C# Nested initialization strangeness

Under the premise that these initialization statements compile List<int> l = new List<int> { 1, 2, 3 }; Dictionary<int, int> d = new Dictionary<int, int>...
Jon Skeet
people
quotationmark

So my question is, why does the expression even compile? It's an object initializer with a collection initializer value. From the C# specification section 7.6.10.2: A member initializer that specifies a collection initializer... more 1/4/2017 2:11:00 PM

people

Java: Why is this a Deadlock when lock is null?

I was having a deadlock scenario in my code. I reproduced the scenario with the following test code (look at the line number comments too). I have one ExecuterService which takes...
Jon Skeet
people
quotationmark

You don't have a deadlock - there's an error just as you expected. The first and second tasks will throw NullPointerException, leaving only the third task running. You can see this if you surround the body of run() in a try/catch... more 1/3/2017 10:59:00 AM

people

C# Skipping parts of code

I got stucked - the first part of the code "// OPEN PRELOADING PAGE" will for some reason not execute and will be skipped. If I comment the rest of the code and only keep this...
Jon Skeet
people
quotationmark

You're calling Response.Redirect at the end of the method. That's going to basically terminate the request with a 302 response. The data you'd previously written to the (buffered) response is irrelevant at that point. Additionally, you... more 1/1/2017 4:17:38 PM

people

Can delegates change parameters in delegate chain? (C#)

We have 1 event, 2 subscribers. When event rises 1st subsriber (function) change parameter and this parameter is of reference type. Will the second subscriber get changed...
Jon Skeet
people
quotationmark

You can't change the parameter to have a different value assuming it's a by-value parameter, but (as always) you may be able to modify the object that the value refers to. Example: using System; using System.Text; class Test { ... more 1/1/2017 12:45:58 PM

people

c# What makes the difference between the two this keywords in the constructor below?

I don't understand the following: In the code found below, Visual Studio tells me, that I can simplify my code by deleting the second this keyword in the constructor. But why...
Jon Skeet
people
quotationmark

The difference is that you've got a parameter called celestialBodyCount - which means that within the constructor, the identifier celestialBodyCount refers to the parameter, so to access the field you have to qualify it with this. You... more 12/31/2016 8:59:04 PM

people