Browsing 7239 questions and answers with Jon Skeet

What the difference between array indexer and any other object indexer

Consider following two data types: class C { public int I { get; set; } } struct S { public int I { get; set; } } Let's try to use them inside the list, for...
Jon Skeet
people
quotationmark

An array access expression is classified as a variable. You can assign to it, pass it by reference etc. An indexer access is classified separately... in the list of classifications (C# 5 spec section 7.1.) An indexer access. Every... more 8/3/2015 3:10:32 PM

people

Override with lambda syntax

Consider the following expression: public override string ToString() => "ABCDEFG"; compiles to this: public override string ToString() { return "ABCDEFG"; } despite...
Jon Skeet
people
quotationmark

Firstly, it's not a lambda expression - it's just that expression-bodied members (introduced in C# 6) also use the syntax of =>. They don't involve creating a delegate instance or an expression tree though. As for why return isn't... more 8/3/2015 10:50:59 AM

people

FileSystemWatcher and GUI

I have a little problem with my WPF project and the FileSystemWatcher class. In my MainWindow class the watcher begins to watch a folder when Button Start is clicked in the...
Jon Skeet
people
quotationmark

Yes, WaitForChanged is a synchronous method: This method waits indefinitely until the first change occurs and then returns. You're calling that from the UI thread - therefore blocking any other UI thread interaction in the meantime.... more 8/3/2015 9:07:38 AM

people

LINQ : How to set value of Take() from a field in current query

I have a table of Widgets with columns "Name" and "Count" The "Count" field contains count of Posts each Widget would show: Name | ...
Jon Skeet
people
quotationmark

It sounds like you just need to fetch the counts first (you may want to cache them): var counts = db.Counts.ToDictionary<string, int>(c => c.Name, c => c.Count); Then: var postsInGallery = postInWidgets .Where(wid =>... more 8/3/2015 8:59:33 AM

people

how to serialize object into json using c#

I tried using var myobject = JsonConvert.SerializeObject(Customer); but problem is in Customer properties are like FirstName and my service expecting json input like...
Jon Skeet
people
quotationmark

You should use the Json.NET attribute support to customize the naming: public class Customer { [JsonProperty("firstName")] public string FirstName { get; set; } } more 8/3/2015 8:49:15 AM

people

Disposing my System.IDisposable object in my finalizer

There are several discussions here on StackOverflow about what to do if my object manages other managed objects that implement System.IDisposable. Note: Below I am not talking...
Jon Skeet
people
quotationmark

The truth is somewhere between the two: The object can't be garbage collected, so the possibility of the object no longer "being there" isn't true An object can be finalized when there are no longer any references to it from other... more 8/3/2015 7:18:33 AM

people

How to properly Clear a Queue containing structs?

I have declared a basic struct like this private struct ValLine { public string val; public ulong linenum; } and declared a Queue like this Queue<ValLine> check =...
Jon Skeet
people
quotationmark

No, you won't have created a memory leak. Calling Clear or Dequeue will clear the memory appropriately - for example, if you had a List<T> then a clear operation might use: for (int i = 0; i < capacity; i++) { array[i] =... more 8/2/2015 6:42:53 AM

people

Indexing XML recursively with xmldocument

i'm trying to add an "index" attribute to all XML nodes using recursion in C#. My problem is that when i try to add an attribute to a node that does not have child nodes, it fails...
Jon Skeet
people
quotationmark

I'd use LINQ to XML for this. My first somewhat inefficient way of doing this would be: foreach (var element in doc.Descendants()) { int indexInLevel = element.ElementsBeforeSelf().Count() + 1; var parent = element.Parent; ... more 7/31/2015 5:05:10 PM

people

Where Debug/Exceptions menu item gone in VS 2015?

Recently I noticed this menu item is gone in my new VS 2015. Is a feature, or just me?
Jon Skeet
people
quotationmark

It's still there, just moved to be its own dockable window instead of a dialog: Debug => Windows => Exception Settings more 7/31/2015 3:25:03 PM

people

Can "this" be null in C# virtual methods? What happens with the rest of instance methods?

I was curious if there is a way for this to be null in a virtual method in C#. I assume it is not possible. I saw that in existing code, during a code review and I would like to...
Jon Skeet
people
quotationmark

It's not possible to do this in normal C# (i.e. calling a method or property in a normal way), regardless of whether the method is virtual or not. For non-virtual methods, you can create a delegate from an open instance method,... more 7/31/2015 1:58:45 PM

people