Tech Tock

Time is of the essence.

4 C# Things You Might Not Have Heard Of

Partial Interfaces

Just learned that Interfaces can be partial.  Not sure how I’ll use this info but it is interesting.

This compiles:

namespace NameSpace
    partial interface IInterface
    partial interface IInterface

Partial Methods

Some folks were unaware that methods can be partial.  But I’ve seen this in my favorite DAL for quite a while.  Its seems most useful in (usually autogenerated) shared code situations as a replacement for virtual functions.  Code calling a partial method is removed from the compiled binary if the method isn’t implemented.  In that way it seems somewhat similar to events as well.

        partial void OnMyMethod();
        private MyMethod()

If OnMyMethod were virtual or an event call, the functionality would be very similar.

Assignment Is A Value

The return value of an assignment expression is the assigned value itself.

            //set both x and y = 5
            int x = (y = 5);

I saw this feature years ago in C/C++ but never thought of using it in C#.


Here’s a use for the return value of assignment with the C# null coalescing operator (another little known feature – coalesce will return the first non null value).  This compresses the code for a common operation while (some would argue) not losing clarity.

private object _aProperty;
public object AProperty
    get { return _aProperty ??
            (_aProperty = new object()); }

January 23, 2010 - Posted by | Uncategorized | , ,


  1. I disagree, every developer should already know about these…

    Comment by zproxy | January 26, 2010 | Reply

  2. […] Hidden or lesser known features? January 26, 2010 Leave a comment Go to comments Aaron blogged about some C# features that aren’t very well known. I won’t call them hidden, actually, as I […]

    Pingback by C#: Hidden or lesser known features? « /dev/random | January 26, 2010 | Reply

  3. I am an avid reader of Lab49 blog and stumbled across your post. Actually, assignment statements do not return a value. Assignment expressions return a value. An assignment expression is a legal statement; Eric Lippert explains this nicely here –

    Comment by Angshuman | October 4, 2011 | Reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: