Browsing 7239 questions and answers with Jon Skeet

goto vs method in finally block in c#

I am particularly aware of C# Reference and have analysed why can't i use goto statement in finally blocks but i wonder when i am trying the same thing with using methods the i...
Jon Skeet
people
quotationmark

Isn't the goto moo and static void moo() doing the same act i.e taking me out of finally block? No, absolutely not. goto moo would transfer control out of the finally block completely. moo() just calls the method, but then when the... more 10/31/2016 4:27:05 PM

people

How to add a continuation for cancelled tasks

I have the following code try { var cancellationTokenSource = new CancellationTokenSource(); var token = cancellationTokenSource.Token; var task = Task.Run(() => ...
Jon Skeet
people
quotationmark

Your first task isn't actually being cancelled - you're observing that cancellation has been requested, but then you're letting the first task complete normally... which means your "only on cancellation" task is cancelled. If you change... more 10/30/2016 1:07:52 PM

people

LINQ .Split(new[] { ' ' }) string and set default value if found null

Here is my LINQ query var listLogOutItems = (from A in data orderby A.FirstName select new { Login = "Logout", Name = A.FirstName + " " +...
Jon Skeet
people
quotationmark

The simplest way is probably to use the null coalescing operator ??. LogoutDate = (A.LogOutTime ?? "Unknown").Split(...)[0] If A.LogOutTime is null, it'll use "Unknown" instead. This is slightly inefficient as it will be calling Split... more 10/30/2016 9:17:31 AM

people

warning CS0618: 'IPAddress.Address' is obsolete: 'This property has been deprecated

warning CS0618: 'IPAddress.Address' is obsolete: 'This property has been deprecated. It is address family dependent. Please use IPAddress.Equals method to perform...
Jon Skeet
people
quotationmark

The documentation for IPAddress.Address says: This property is obsolete. Use GetAddressBytes. So I suggest you do that: public static long CastIp(string ip) { IPAddress address = IPAddress.Parse(ip); byte[] addressBytes =... more 10/29/2016 10:19:14 AM

people

c# async task cancellation

I have some problems getting a grasp on tasks and cancellation tokens. I've made a program that looks like this: static void Main(string[] args) { CancellationTokenSource...
Jon Skeet
people
quotationmark

None of the tasks get ever cancelled when I run the program. That's because you're only ever checking the cancellation token at the start of the task. Once it's got past that first token.IsCancellationRequested check, cancelling the... more 10/28/2016 1:03:12 PM

people

How to add more query on to the end of an IEnumerable

I would like to pass part of the query through as a parameter of a method. public IEnumerable<ServiceModel> Get(/*What would the type be*/ rightQuery) { ...
Jon Skeet
people
quotationmark

It sounds like you just want a Func<IEnumerable<ServiceModel>, IEnumerable<ServiceModel>> - in other words, "Given one query, I'll give you another." public IEnumerable<ServiceModel> Get( ... more 10/27/2016 9:35:42 PM

people

My C# search function isn't finding key words when it should

I'm trying to perform a search for multiple key words. But even when I know it should find the word, it isn't. private void btnSearch_Click(object sender, RoutedEventArgs e) { ...
Jon Skeet
people
quotationmark

Currently you're looking in the strings "win810Words" and "win7Words". There's an enormous difference between those string values and the values of the variables win810Words and win7Words. So the first change is to use: string[]... more 10/27/2016 4:45:29 PM

people

Why does the result of IsGenericType differ in these examples?

I have functions, which generate collection of char objects: public static IEnumerable<char> generic_foo<T>() { return "1231"; } public static...
Jon Skeet
people
quotationmark

generic_foo2() is implemented by the compiler via a state machine. That state machine will be generic in T even though you're not actually using it - so calling GetType() on the instance of the state machine will give a generic type. It... more 10/27/2016 2:58:35 PM

people

project.json Equivalent of InternalsVisibleTo

.Net Core's project.json allows configuration of various assembly properties (e.g. title, version, copyright) that a traditional .Net application would define using attributes...
Jon Skeet
people
quotationmark

No - just put it in AssemblyInfo.cs as normal. That's still a perfectly fine place to put assembly attributes. If one isn't created for you in the template you're using, just create your own AssemblyInfo.cs file (or any other name, of... more 10/27/2016 1:42:53 PM

people

c# return json result as an array

I am making a WebApi application, and trying to get my controller to return a Json object result that is within an array. I want my response to be formatted like this: [ { ...
Jon Skeet
people
quotationmark

Well yes, you're just creating a new anonymous type. It's easy to wrap that in an array though: return Json(new[] { new { stampCode = "666", email = "666_doctor@gmail.com", phone = "+370 640 000000", healthCareProvider =... more 10/27/2016 9:02:10 AM

people