Browsing 7239 questions and answers with Jon Skeet

How to read the data of generic list without iterate it?

I have create a list like this: List<int> list = new List<int>(); list.Add(2); list.Add(3); list.Add(7); now if I want iterate it I should do: foreach (int prime...
Jon Skeet
people
quotationmark

You can do it using List<T>.ForEach: list.ForEach(Console.WriteLine); This employs a method group conversion to create an Action<int> to call Console.WriteLine(int) for each element of list. However, personally I would use... more 2/28/2016 1:46:19 PM

people

Converting char "a" to number 0 using java function

So I'm learning about functions and methods, and trying to create a function that would allow me to replace a Letter with a Number, thus "a" would be 0, "b" would be 1, so on and...
Jon Skeet
people
quotationmark

The simplest approach is just to subtract the literal 'a'... which will implicitly convert both your input letter and the 'a' to int: public int convert(char letter) { if (letter < 'a' || letter > 'z') { throw new... more 2/27/2016 12:15:08 PM

people

How to get the index of each item that is null in a List<T> via Linq

Is there a way to get the index of each object in a List that is null? For example: List<string> list = new List<string>() { "1", null, "2", null, "3" }; Is it...
Jon Skeet
people
quotationmark

Yes, the simplest option would probably be: var nullIndexes = list.Select((value, index) => new { value, index }) .Where(pair => pair.value == null) .Select(pair => pair.index) ... more 2/27/2016 11:10:31 AM

people

Double await in one call

I was watching a video called Becoming a C# Time Lord and at 0:35:36 this code popped up: async Task<TResult[]> PurelyWhenAll<TResult> (params...
Jon Skeet
people
quotationmark

Task.WhenAny is going to return a Task<Task<TResult>>: Awaiting the result of Task.WhenAny() will return the first task that completed Awaiting that task will return the results of the task, i.e. a TResult[]. You might find... more 2/27/2016 10:37:03 AM

people

If I have a property do I need a field as well?

So after the availability of automatic implementation, and initialization of a property, do I even need a field for my property? This seems much cleaner: class A { public int X...
Jon Skeet
people
quotationmark

In the first case, the compiler is already providing a backing field - it's just implicit (and it's given a name that you can't refer to in code). Note that there has to be a backing field in the generated code, as a property itself is... more 2/27/2016 9:39:52 AM

people

Creating a StreamWriter object throws an error: "The type String could not be converted to type Stream"

I need to use a StreamWriter to write to a text file, but when I try to create one it throws this error: Argument 1: cannot convert from 'string' to 'System.IO.Stream' On...
Jon Skeet
people
quotationmark

I suspect you're writing a portable class library or something like that - if you look at the list of supported platforms in StreamWriter(String) it's considerably smaller than the list of supported platforms in StreamWriter(Stream). I... more 2/27/2016 6:55:00 AM

people

Cancel ONLY one task with CancellationToken

Lets assume that we have a Producer-Consumer pattern created with One Producing Task and 3 Consumer Tasks as follow: Task[] Consumer = new Task[10]; for (int i =...
Jon Skeet
people
quotationmark

If you need to cancel all the consumers independently, you need separate cancellation tokens - and thus separate cancellation token sources. var consumers = Enumerable .Range(0, 10) .Select(_ => new... more 2/26/2016 11:30:24 PM

people

TimeSpan utc0 to timezone and some days

Hi i have some table with starttime end time and timezone I get data in DateTime.Utc http://prntscr.com/a84tr3 And how i can check if the date what comes is get caught in this...
Jon Skeet
people
quotationmark

Firstly, it's important to understand that what you've got isn't a time zone. It's a UTC offset (although the negation of what it would normally be...). A real time zone would need to indicate how that UTC offset varies over time (e.g. due... more 2/26/2016 11:51:28 AM

people

Convert asynchronous action to asynchronous function delegate, preserving synchronous exception delivery

I want to convert asynchronous action delegates to asynchronous function delegates that return a specified value. I have come up with an extension method for this: public static...
Jon Skeet
people
quotationmark

Well, you won't be able to use async in the initial call. That much is clear. But you can use a synchronous delegate which calls the function, and then captures the returned task to await it inside an async delegate: public static... more 2/25/2016 5:17:51 PM

people

Assigning Final variables from constructors | not allowed from methods. Why?

Practicing some code samples and i have came across this: I have declared final variables at class level and trying to assign values from methods, lead to compile time...
Jon Skeet
people
quotationmark

Constructors are executed once. Methods can be executed multiple times. Assignments to final variables are only permitted once - it's as simple as that. (If they could be assigned different values after construction, they wouldn't be very... more 2/25/2016 11:18:54 AM

people