Browsing 15836 questions and answers with Jon Skeet

Error passing both a dynamic object and an Action to a function

I am trying to pass a dynamic object and an Action into a function. (below is a simple test) But, I am getting the following compile time error: Cannot use a lambda...
Jon Skeet
people
quotationmark

Sure - do exactly as the compiler says - cast the lambda expression to a concrete type: Calc(obj, (Action<int>)(result => Console.Write("Result: " + result))); The reason you have to do this is that a lambda expression doesn't... more 8/15/2017 4:00:54 PM

people

Initialize an object before an if statement

in my program I am reading in an excel sheet and am doing some linq selections, which worked fine. Problem: I tried to make a preselection by applying an If-statement. The...
Jon Skeet
people
quotationmark

I see three options, although there are others: You could explicitly declare the type of variable you want, in which case you can initialize it separately in each case: IQueryable<LinqToExcel.Row> discounts; if... more 8/15/2017 3:26:29 PM

people

Array types with same element type & rank not equal

Very simple: var equal1 = typeof(object[]) == typeof(object).MakeArrayType(); var equal2 = typeof(object[]) == typeof(object).MakeArrayType(1); var equal3 = typeof(object[,]) ==...
Jon Skeet
people
quotationmark

The documentation explains the difference: The common language runtime makes a distinction between vectors (that is, one-dimensional arrays that are always zero-based) and multidimensional arrays. A vector, which always has only one... more 8/15/2017 1:33:19 PM

people

How to reference .NET framework from ASP.NET Core

I have a ASP.NET MVC 5 / .NET 4.7 project that I want to upgrade to ASP.NET Core. The MVC frontend references a service layer project (SL) which in its that uses a datalayer...
Jon Skeet
people
quotationmark

What framework should my projects target to be able to work together? Can we upgrade just one project to .NET Core and keep the rest in .NET Framework? You don't need to make any of your projects target .NET Core in order to use... more 8/15/2017 1:00:41 PM

people

Warning does not implement the 'collection' pattern

I am making collection, implementing IEnumerable explicitly and trying to iterate it from within: public class MyCollection<T> : IEnumerable<T>, IEnumerable { ...
Jon Skeet
people
quotationmark

The warning exists because the C# compiler can handle foreach in a number of different ways. One of those ways is to find a GetEnumerator method with a suitable return type. That's checked before the compiler checks whether or not the type... more 8/15/2017 12:43:41 PM

people

Struct extension methods

With code: someVector.FixRounding(); //round vector's values to integers if the difference is 1 epsilon float x = someVector.x; //still getting old value public static void...
Jon Skeet
people
quotationmark

This declaration: public static void FixRounding(this Vector3 v) ... means v is being passed by value, and it's a struct, assuming the documentation is correct. Therefore any changes you make to it won't be visible to the caller. You... more 8/14/2017 5:54:23 PM

people

Java PrintWriter File Overwrite

I want to write into a file using UTF-16 so I use PrintWriter(file,"UTF-16"), but then it deletes everything in the file, I could use FileWriter(file,true), but then it wouldn't...
Jon Skeet
people
quotationmark

Use OutputStreamWriter with a UTF-16 charset, wrapping a FileOutputStream opened with append=true. Alternatives, use Files.newBufferedWriter: try (Writer writer = Files.newBufferedWriter( Paths.of("filename.txt"), ... more 8/14/2017 3:26:27 PM

people

Using a generic (X) delegate in a generic (Y) method where Y extends some base class. Cannot convert to base class

public abstract class SomeBaseClass {} public class SomeSpecificClass: SomeBaseClass { public int propertyA; public string propertyB; } public delegate void...
Jon Skeet
people
quotationmark

Basically, you can't do this because it's not safe. Suppose we have a concrete class derived from SomeBaseClass: public class SomeOtherSpecificClass {} Suppose we change your MethodB to: void MethodB(Callback<SomeBaseClass>... more 8/14/2017 11:10:19 AM

people

java jsp if statement

Following code gives me a big headache. Why does my if ignore rs.next() == true? System.out.println(rs.next()); if (rs.next() == true) { ...
Jon Skeet
people
quotationmark

What am I doing here wrong? You're calling next() twice. The first call returns true, but presumably the second returns false - which makes sense, if your query only returns a single result. If you really want to print the value out... more 8/13/2017 5:37:41 PM

people

Unchecked assignment: 'anonymous java.util.Iterator' to 'java.util.Iterator<java.lang.Integer>'

I have a class like this public class Symbol implements Iterable<Integer> { private int n = 123; public Iterator<Integer> iterator() { return new...
Jon Skeet
people
quotationmark

All you've got to do is change your anonymous class to use Iterator<Integer> instead of the raw type: return new Iterator<Integer>() { // Code here as before }; Imagine it had been written like this: public... more 8/13/2017 2:53:54 PM

people