This is based on a recent thread on the MSDN forums. Someone had code that looked like this: public class CustomObject<T> { Object _obj; public Object Value { get { return _obj; } } public static explicit operator CustomObject<T>(T obj) { return new CustomObject<T>() { _obj = obj }; } } class App { static [...]
Entries Tagged as 'C#/.NET'
An interesting issue with generics, casting, and explicit/implicit operators
June 28th, 2010 · No Comments
Tags: C#/.NET
Type.ToString quirk on Array types
June 25th, 2010 · No Comments
There was a recent thread in the MSDN forums where someone was concerned about the behavior of Type.GetElementType. His example was similar to the following: char [][,][] arr = new char[2][,][]; Console.WriteLine(arr.GetType().GetElementType()); The output he got was: System.Char[][,] This puzzled him no end because the element type is clearly char[,][]. He was wondering if this [...]
Tags: C#/.NET · CLR/.NET BCL
FAQ: C# calling C++ code with callbacks with unmanaged array arguments
June 11th, 2010 · No Comments
This question or a similar variation pops up in the forums once in a while. The core problem is that while null terminated char arrays can be marshaled to a System.String fairly easily, with other unmanaged arrays the marshaller cannot know for sure what size of array to create. The solution is to marshal the [...]
Article: Handling a Window’s Closed and Closing events in the View-Model
April 16th, 2010 · No Comments
This article discusses an attached behavior that lets you handle the View Window’s Closed and Closing events via commands in the View-Model. It was inspired by Reed Copsey, Jr.’s Blend behavior which is up on the Expression Code Gallery. Reed’s behavior uses a neat technique that lets the View-Model handle the Closing/Closed events of the [...]
Tags: C#/.NET · WPF (Avalon)
Article : EXIF Compare Utility using WPF
April 12th, 2010 · No Comments
The Exif Compare Utility is a WinDiff equivalent for image files that compares the Exif meta-data and displays the differences and similarities. The application is written using WPF and MVVM, and also makes use of my ExifReader library. In the article I briefly explain how to use the application and also discuss some of the [...]
Tags: C#/.NET · WPF (Avalon)
Article : An MVVM friendly approach to adding system menu entries in a WPF application
April 5th, 2010 · No Comments
The majority of MFC apps have always had an About… menu entry in the main window’s system menu, and this was primarily because the App Wizard generated code for that by default. I wanted to do something similar in a WPF application I’ve been working on, and I wanted to do it in an MVVM [...]
Tags: C#/.NET · WPF (Avalon)
An attached behavior for WPF TextBoxes to force instant binding
April 3rd, 2010 · 3 Comments
Update – Apr 9, 2010 You do not need to do all this – instead you just need to set UpdateSourceTrigger on the Binding to PropertyChanged. I’ve made an updated blog entry on this here : Correction for the instant binding attached behavior for WPF TextBoxes Original entry follows for posterity The default behavior for [...]
Tags: C#/.NET · WPF (Avalon)
Article : An extensible ExifReader class with customizable tag handlers
March 30th, 2010 · No Comments
I needed an Exif reader class for a C# application I was working on, and though I found quite a few implementations available including a few on The Code Project, none of them fully suited my requirements. So I wrote my own class. The article describes the use and implementation of this class, and also [...]
Tags: C#/.NET · WPF (Avalon)
An attached behavior for TextBoxes to support TextChanged commands
March 22nd, 2010 · 1 Comment
Recently I was working on an MVVM demo for an article where I wanted live filtering based on the contents of a text box. The problem with this is that the TextBox does not have a Command property, and thus I’d need to use code-behind to proxy its text changed event to the ViewModel. And [...]
Tags: C#/.NET · WPF (Avalon)
Undocumented compiler error CS0226
February 28th, 2010 · No Comments
This one’s an obvious error really – but it’s still undocumented, and thus qualifies for a blog entry. I guess once __arglist was dropped from the standard documentation, all its associated error messages were dropped too. Compiler Error CS0226 Error Message An __arglist expression may only appear inside of a call or new expression Example [...]
Undocumented compiler error CS0224
February 26th, 2010 · No Comments
A fellow CPian leppie had posted about an undocumented C# error he got (error CS0224) and asked if anyone could reproduce the error (back in Dec 2009). For fun I played with the compiler and managed to reproduce the error. That gave me the idea of blogging about this and other such undocumented errors. Of [...]
Tags: C#/.NET
Consuming a WCF service using a native WWS client
July 23rd, 2009 · 4 Comments
I have been meaning to play with Windows Web Services ever since I heard Nikola Dudar talk about it at the MVP Summit earlier this year. It’s natively included with Windows 7, but can also be installed and used from older OSes (XP, Vista, 2003 and 2008). You can write native clients using WWS that [...]
Tags: C#/.NET · C++ · Indigo · WWS API
.NET 4.0 : Arrays are now structurally equatable
July 20th, 2009 · No Comments
In .NET 4.0, arrays implement IStructuralEquatable which lets you equate two arrays based on their contents. int[] array1 = new int[] { 3, 5, 9 }; int[] array2 = new int[] { 3, 5, 9 }; IStructuralEquatable equatableArray1 = array1; Console.WriteLine( equatableArray1.Equals( array2, EqualityComparer<int>.Default)); The above code outputs True. You can use it with your [...]
Tags: C#/.NET · CLR/.NET BCL
Article : Delayed Delegate Invoker
July 15th, 2009 · 1 Comment
Here’s my latest article that discusses a simple utility class that lets you queue up delegates and then execute them at a later time in a first-in first-out order. I wrote this when I had a situation where I was doing some rather complex processing based on property changed event notifications, and I found that [...]
Tags: C#/.NET
A boolean lock using IDisposable
June 23rd, 2009 · 6 Comments
While there are more complex and functional lock mechanisms, I wanted a very simple lock that would tell me if an operation was locked or not. Normally I would just have a flag that’d be true or false, but I found that it was not always easy to remember to set the flag on or [...]
Tags: C#/.NET
Checking for Design-Mode
June 21st, 2009 · 2 Comments
The DesignMode property does not always return the correct value, specially for nested controls or for child controls instantiated in their parent control’s constructors. One workaround is to check for LicenseManager.UsageMode and see if it’s equal to LicenseUsageMode.Runtime, but even that won’t work all the time. It will always return Runtime from event handlers and [...]
Tags: C#/.NET · Windows Forms
Exposing List<T> vs IList<T>
June 14th, 2009 · 4 Comments
Unless you specifically want to expose a List<T> via a public interface (either as a return type or as a method argument), it’s a better idea to expose an IList<T>, ICollection<T>, or even an IEnumerable<T> depending on your needs. Recently I had to implement a generic list class that implemented property notifications and for this [...]
Tags: C#/.NET
C# 4.0 and variant generic delegates
May 29th, 2009 · 1 Comment
Continuing on from my previous blog entry, in this entry I will talk about variant generic delegates. Just as with generic interfaces, generic delegates can use out and in to specify covariant and contravariant parameters respectively. And the MSIL equivalents are the + and – symbols. Here are three variant generic delegate declarations: delegate T [...]
Tags: C#/.NET
C# 4.0 and variant generic interfaces
May 27th, 2009 · 2 Comments
The CLI supports variant generic parameters for interfaces as well as delegates, and C# 4.0 has added support for that. In this blog entry I’ll talk a little about variance in generic interfaces, and in a later entry I will talk about variance in delegates. Covariance allows you to use a more derived type than [...]
Tags: C#/.NET
Named and optional parameters in C# 4.0
May 25th, 2009 · 3 Comments
One commonly heard grievance about C# was how it did not support optional parameters. Well C# 4.0 not only has that, but you can also specify parameters by name. Consider the following two examples : public void RepeatText(string text, int count = 3) { while (count– > 0) { Console.WriteLine(text); } Console.WriteLine(); } public void [...]
Tags: C#/.NET