Linq To Objects: Anonymous Types and Methods

One of the core language features that C# relies on heavily when working with Linq is implicit typing. Implicit typing allows users to create anonymous types, which are simply types constructed at compile time based on the RHS expression specified by using the new keyword in conjunction with the object initializer syntax.

Implicit typing also works with predefined types, however it’s important to note the difference between the two syntactically similar statements, as they yield very different results:

var a = new int [] { 42 };

Create a new instance of a predefined type – an array containing a single element with the integer value ’42’.

var b = new { a =  42 };

Creates a new compiler generated type – consisting of a single field, decorated with the value ‘a’, of the integer type. This instance contains the value ’42’ in its ‘a’ field.

The rest of the post will focus solely on the former construct.

Continue reading

Native and Managed Interoperability: DirectDraw via CLI / C++

In my previous post on Native and Managed Interoperability I briefly mentioned that “Managed C++” has it’s own unique interoperability features. Technically, the “Managed C++” syntax is officially depreciated and has been superseded by CLI / C++, or the Common Language Infrastructure.

However, the same underlying concepts still apply. CLI / C++ is a very powerful language that allows developers to bridge the gap between managed BCL components and native components under a single managed context.

Continue reading

Rendering Framework: GDI via C#

When developing graphical applications in a Windows environment, one of the key fundamental aspects for creating rich applications is understanding the “Graphics Device Interface” or, more commonly known as, simply GDI.

GDI is an abstraction layer for accessing video hardware built into the Windows operating system, specifically residing in the “GDI32.dll”.

This abstraction layer is responsible for drawing geometrical shapes, text with various fonts and sizes, handling various color depths – most commonly 8-bpp (256 colors represented by a palette – indexed, monochrome or “real color”), 16-bpp (“high color”), 24-bpp (“true color”) and 32-bpp ( “true color with transparency”) and rendering results to a Device Context – such as a screen or a printer.

Continue reading

Rendering Framework: Real-time Drawing with C#

One of the first things that truly broadened my horizons on how amazing software development could be, was when I saw my first “Demo”. A demo is simply a non-interactive real-time display of a visualization effect. Some of the most classic (and simple) examples are the Starfield, Plasma, Fire and Water. The real appeal of these first demos was that developers, on basically identical hardware, could really display their algorithm optimization talents – some going as far as exploiting hardware defects to achieve faster rendering routines.

Continue reading

Native and Managed Interoperability: Platform Invoke

When I first began learning how to write software it was on a Windows 95 box using Visual Basic 3. Visual Basic provided a great learning tool for getting into development. It doesn’t have a high price for entry, as just about anyone with a little inclination can pick it up and use the RAD tools it provides. However, I eventually grew of it and moved to learning C and the working with the Win32 API directly. Ever since, I have been absolutely hooked. I really enjoy working with the native Windows API (yes, I’m one of the few), so naturally when I started picking up managed languages I was immensely curious about the power of interoperability.

Continue reading