February 6th, 2008

LinQ

About a year ago I said I would make a post about LinQ. Now that Visual Studio 2008 is out, and LinQ is a released technology, I think I can get around to it. I still haven’t built a project that makes a good use of LinQ, but I’ve got a fairly good understanding of it, and found some interesting ways to use it.

Now, you may be wonder what LinQ is, and what it stands for. The answer is that it stands for Language INtegrated Query. It is an extension to C# and VB.NET that allows you to use a SQL-like syntax for constructing queries against a wide variety of data sources. The most obvious example is XML, but you can also use most of the built in data structures in the .NET framework as well.

With that out of the way, on to an example or two of how I’ve managed to use LinQ, and what the challenges that I’ve run into are.
Read the rest of this entry »

February 22nd, 2007

C#: Programming Made Fun

I realize I’m rather off the schedule I had planned. I blame the holiday weekend, my laziness, and Robocup. The international qualification deadline is at midnight on February 22nd. For those of you unaware of what the date is, that is in less than 24 hours (it’s 2:15am at the moment). That means it is somewhere close to crunch time, which means that I’ve been working. The new look for the RFC Cambridge site was rolled out tonight, and I’ve been helping with the team description paper (both writing sections, and proofreading it). With that out of the way, on to the actual topic of this post…

C# is one of the primary languages supported with the .NET Framework. C# is a strongly typed, managed language. This means that the language runtime is responsible for memory allocation and deallocation (garbage collection is the technical term). This saves a lot of hassles and helps to reduce (but not eliminate) memory leaks (memory that has been allocated, but is no longer pointed to, but not deleted). The .NET Framework is also important, as it provides C# with a powerful class library.

C# also has the capability of using essentially the entire power of C++ through something called Platform Invoke (or PInvoke for short). PInvoke allows you to call essentially arbitrary C++ (with a few requirements for how it is written) from within C#. This is hugely powerful, as you can access unmanaged memory, and do operations that are inherently more efficient in C++ than in C#. There are also a few features of C++ that aren’t available in C# (such as memory mapped files) which are useful.

Another powerful capability of C# is called reflection. Reflection is essentially the ability to examine and modify objects at runtime. This allows you to determine the capability of an object, and even to extend it programmatically, at run time, rather than at compile time. C# can also programmatically invoke a C# compiler (through the .NET framework) which can allow for things like dynamically compiled modules, and other interesting features. With Reflection, you can then discern properties of the runtime compiled code, and integrate it into an existing system. This allows for a theoretically powerful plugin system, which can at the same time be limited for security purposes. When compiling code at runtime you can specify the assemblies (code components more or less, some of these terms are almost another post in themselves). This can lead to some very interesting ideas (an XML Parsing system based solely on object structures, or an easily extensible editor are a couple that I’ve had), and allows for very powerful capabilities.

The last, and one of my favorite features of C# (well, more correctly Visual Studio) is the GUI (Graphical User Interface, ie what you see) designer. I originally began programming with Visual Basic (save the hate, I’ve moved on to “better” languages), and I always enjoyed the ease of building a GUI with Visual Basic, even if the language behind it was a little quirky. It was actually common to use Visual Basic to build a GUI, and use COM (Component Object Model, a rather nasty complicated Microsoft technology) to hook up C++ code for the actual program component. With C# you don’t have to go that far, as the GUI designer is part of C# as well.

This is getting long enough, even though there is a lot more to cover. I guess this will have to become a multiple part series (to be continued on Friday I suppose). I’ll close with a link to Visual C# Express Edition, which is a free fully featured C# IDE (Integrated Development Environment, I need a little extension to let me just link to wiki pages for these) for Windows. It’s fun to play with, and gives you most of the power of Visual Studio 2005 for free.