Browsing 15901 questions and answers with Jon Skeet

c# FileStream Read having problems with StreamReader EndOfStream

As the title says I found a problem. Little back story first: We have file.txt looking like this: aaaabb ccccddd eeeefffffff There are many ways to read this text line-by-line,...
Jon Skeet
people
quotationmark

Now there's a problem. Now arr doesn't get anything and s reads the whole line including the first 4 letters. Yes, that seems very plausible. StreamReader maintains a buffer - when you ask it to read a line of text, it may well read... more 8/18/2017 7:32:22 AM

people

Generate random boxed co ordinates in c#

I need to find random co-ordinates, bound like a square. For this I defined this where the values 70, 55, 175, 175 are the furthest points I want to go to: north =...
Jon Skeet
people
quotationmark

I would suggest you change your approach: Generate a point for the North/West corner, anywhere in the appropriate range Generate the width and height, ensuring they're positive Set East = West + Width, and South = North - Height more 8/18/2017 6:29:52 AM

people

prepare a set of arguments that can pass to a function in java

I want to call a function like private void passStrings(String... arg){} And I have an array which store a set of Strings but don't know its size. So How can I use all that array...
Jon Skeet
people
quotationmark

Just pass it as the array: String[] array = { "Some", "arguments", "I", "prepared", "earlier" }; passStrings(array); A varargs parameter like arg is still an array parameter really - it's just that the compiler allows you to specify the... more 8/18/2017 6:04:05 AM

people

The current .NET SDK does not support targeting .NET Standard 2.0 error in Visual Studio 2017 update 15.3

I want to create a class library project with Target Framework .NET Standard 2.0. I've updated my Visual Studio 2017 to Version 15.3 and also in Visual Studio installer checked...
Jon Skeet
people
quotationmark

It sounds like installing the VS2017 update didn't also install the .NET Core 2.0 SDK. You can download that here. To check which version of the SDK you've already got installed, run dotnet --info from the command line. Note that if... more 8/17/2017 2:22:58 PM

people

DateTime Manipulation in C# vs Java

I'm new to Java. I have a time I am getting from a web-page, this is in the "hh:mm" format (not 24 hour). This comes to me as a string. I then want to combine this string with...
Jon Skeet
people
quotationmark

Is there a better way to achieve what I want? Absolutely - in both .NET and in Java, in fact. In .NET I'd (in a biased way) recommend using Noda Time so you can represent just a time of day as a LocalTime, parsing precisely the... more 8/16/2017 12:27:05 PM

people

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