How to write Unicode characters to the console?

It’s likely that your output encoding is set to ASCII. Try using this before sending output: Console.OutputEncoding = System.Text.Encoding.UTF8; (MSDN link to supporting documentation.) And here’s a little console test app you may find handy: C# using System; using System.Text; public static class ConsoleOutputTest { public static void Main() { Console.OutputEncoding = System.Text.Encoding.UTF8; for (var … Read more

ObservableCollection Doesn’t support AddRange method, so I get notified for each item added, besides what about INotifyCollectionChanging?

Please refer to the updated and optimized C# 7 version. I didn’t want to remove the VB.NET version so I just posted it in a separate answer. Go to updated version Seems it’s not supported, I implemented by myself, FYI, hope it to be helpful: I updated the VB version and from now on it … Read more

“Items collection must be empty before using ItemsSource.”

I had this same error for a while in a slightly different scenario. The cause was invalid XAML, because some tags were missing. I had <wpftoolkit:DataGrid AutoGenerateColumns=”False” ItemsSource=”{Binding Path=Accounts}” > <wpftoolkit:DataGridTextColumn Header=”Account Name” Binding=”{Binding Path=AccountName}” /> </wpftoolkit:DataGrid> which I fixed to be <wpftoolkit:DataGrid AutoGenerateColumns=”False” ItemsSource=”{Binding Path=Accounts}” > <wpftoolkit:DataGrid.Columns> <wpftoolkit:DataGridTextColumn Header=”Account Name” Binding=”{Binding Path=AccountName}” /> </wpftoolkit:DataGrid.Columns> … Read more

Setting Objects to Null/Nothing after use in .NET

Karl is absolutely correct, there is no need to set objects to null after use. If an object implements IDisposable, just make sure you call IDisposable.Dispose() when you’re done with that object (wrapped in a try..finally, or, a using() block). But even if you don’t remember to call Dispose(), the finaliser method on the object … Read more

Is there a VB.NET equivalent for C#’s ‘??’ operator?

Use the If() operator with two arguments (Microsoft documentation): ‘ Variable first is a nullable type. Dim first? As Integer = 3 Dim second As Integer = 6 ‘ Variable first <> Nothing, so its value, 3, is returned. Console.WriteLine(If(first, second)) second = Nothing ‘ Variable first <> Nothing, so the value of first is … Read more

What exceptions should be thrown for invalid or unexpected parameters in .NET?

I like to use: ArgumentException, ArgumentNullException, and ArgumentOutOfRangeException. ArgumentException – Something is wrong with the argument. ArgumentNullException – Argument is null. ArgumentOutOfRangeException – I don’t use this one much, but a common use is indexing into a collection, and giving an index which is to large. There are other options, too, that do not focus … Read more

Which is generally best to use — StringComparison.OrdinalIgnoreCase or StringComparison.InvariantCultureIgnoreCase?

Newer .Net Docs now has a table to help you decide which is best to use in your situation. From MSDN’s “New Recommendations for Using Strings in Microsoft .NET 2.0” Summary: Code owners previously using the InvariantCulture for string comparison, casing, and sorting should strongly consider using a new set of String overloads in Microsoft … Read more

Is there a way to navigate to real implementation of method behind an interface?

I do the following: 1) Right click on the method and click “View call hierarchy” (or shortcut Ctrl+K, Ctrl+T) 2) Expand the “Implements x” folder which will then show you all the implementations of that method. Click on one to go there. Relatively quick and easy. Annoyingly though there doesn’t seem to be an equivalent … Read more