C# – Undocumented Keywords

While browsing the code of the interlocked class I stumbled upon the following piece of code

public static T CompareExchange<T>(ref T location1, T value, T comparand) where T : class
{
    // _CompareExchange() passes back the value read from location1 via local named 'value'
    _CompareExchange(__makeref(location1), __makeref(value), comparand);
    return value;
}

Further googling revealed that there are actually 4 undocumented keywords in C# __arglist, __refvalue, __makeref, __reftype

More on the subject can be found on codeproject: http://www.codeproject.com/Articles/38695/UnCommon-C-keywords-A-Look

The C# Memory Model in Theory

The C# memory model makes and shows the code patterns that motivate the guarantees; the second part will detail how the guarantees are achieved on different hardware architectures in the Microsoft .NET Framework 4.5.

One source of complexity in multithreaded programming is that the compiler and the hardware can subtly transform a program’s memory operations in ways that don’t affect the single-threaded behavior, but might affect the multithreaded behavior.

Original:
http://msdn.microsoft.com/en-us/magazine/jj863136.aspx

Uploading file to Google Drive with C# code

Software, Internet and related

I had to host some files on Google Drive and they needed to be updated on regular basis. Going through the GUI is a tedious task if repeated often, so I ended up coding my own upload code.

The upload login is just using the Google SDK, but the tricky part is the authentication with the Google Services, which is OAuth 2.0 ticket base and needs interaction at least for the first run of the program.

Here is the Google Drive encapsulated logic. It exposes methods for listing the drive contents, downloading and uploading:

And here is the application logic. It’s just a command line utility that accepts the client Id and secret as first parameter and the file to be uploaded as third:

And the rest is the most tricky part. The authentication:


View original post

Showing tooltip on textbox when text exceeds the available size of textbox(Attached Behavior in WPF)

XAML TAG

WPF provides an innovative feature called attached properties, which can be used to add behavior to existing controls. These new properties are not defined on the control being extended, but rather they are defined on a separate object (generally a
DependencyObject). Thus, we end up with a source object that defines the attached properties and a target object on which you attach these properties. The target is the object being extended with new functionality contained in the source. When the attached property is set on the target object, a property change event is fired on the source. The event handler is passed information about the target and the new and old values of the property.
By hooking up property changed handlers, we can add additional behavior by calling methods on the target, changing properties, and listening to events. We look at more detailed examples of using attached properties in Chapter 6, “The Power of Attached Properties.” Keep in mind that…

View original post 392 more words

Thread Safe Generic Weak Dictionary in .NET

If you are about to begin implementing your own version of a thread safe generic weak dictionary – STOP!

As of .NET 4.0, there is already one:

System.Runtime.CompilerServices.ConditionalWeakTable<TKey, TValue>

Now that .NET 4.5 has been made open source few weeks ago, we can look at the comments and more importantly at the implementation of ConditionalWeakTable<TKey, TValue>. Continue reading “Thread Safe Generic Weak Dictionary in .NET”

Design patterns and practices in .NET: the Chain of Responsibility pattern

Exercises in .NET with Andras Nemes

Introduction

The Chain of Responsibility is an ordered chain of message handlers that can process a specific type of message or pass the message to the next handler in the chain. This pattern revolves around messaging between a sender and one more receivers. This probably sounds very cryptic – just like the basic description of design patterns in general – so let’s see a quick example in words.

Suppose we have a Sender that knows the first receiver in the messaging chain, call it Receiver A. Receiver A is in turn aware of Receiver B and so on. When a message comes in to a sender we can only perform one thing: pass it along to the first receiver. Receiver A inspects the message and decides whether it can process it or not. If not then it passes the message along to Receiver B and Receiver B will perform the…

View original post 1,152 more words

Formatting Cheat Sheet

No credit should be given to me, copied from color-of-code

Syntax

Formatting is bound to a type, so depending on which type of object you pass to String.Format, you have different format specifiers.

The index specifies the position of the parameter that shall be used for that format specifier. It can be repeated!

When implementing custom formatting, try to stick to this behaviour for being consistent with the default implementation.

  • Generic, alignment is applied after type formatting, so can be used for all types
    • {index[,alignment][:format]}
  • For numerical values (; separator):
    • {index[,alignment]:format >= 0;format < 0}
    • {index[,alignment]:format > 0;format < 0;format = 0}

Pitfalls and gotchas Continue reading “Formatting Cheat Sheet”

Blog at WordPress.com.

Up ↑