<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4464677400982090871</id><updated>2011-07-29T08:35:47.064+01:00</updated><category term='C++'/><category term='C#'/><category term='performance'/><title type='text'>Steve's Developer Page</title><subtitle type='html'>My experiences as a developer and trainer using C#, Visual Basic, SQL, ASP.Net, JavaScript and sometimes C++, Excel VBA, PHP and MySQL.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://blog.tgn.co.uk/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4464677400982090871/posts/default'/><link rel='alternate' type='text/html' href='http://blog.tgn.co.uk/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>stevej</name><uri>http://www.blogger.com/profile/16907553839195552660</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>6</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4464677400982090871.post-4155445880702079966</id><published>2010-05-25T09:24:00.003+01:00</published><updated>2010-05-25T09:36:29.822+01:00</updated><title type='text'>Has VB come of age?</title><content type='html'>&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;When .Net first came out I tended to use C# by choice, having a history of Java and C++, but I still retained an affection for Visual Basic.&lt;/span&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Most things could be done in VB rather than C#, but VB was always a little more verbose and had annoying features like having to type in space-underscore to carry a statement onto another line.&lt;/span&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Provided strict type checking was turned on though it gave the same performance as C#, and of course having been a Java enthusiast strict type checking in itself was very important to me.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;This week I wanted to begin a project involving various forms of parallel execution.&lt;/span&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;In the past I would have instinctively gone for C#, but having been training in VB for the previous couple of weeks I decided to begin with VB.&lt;/span&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;And I found myself rather liking it, especially now I don’t need the space-underscores any more.&lt;/span&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;I guess the first thing is that being able to do parallel programming naturally in VB was revealing – something that would have been assumed to be C++ territory a decade ago.&lt;/span&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;But I also found that small changes from the last couple of versions appealed.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;For example, let’s imagine that you are issuing an ADO.Net command and getting a data reader back.&lt;/span&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;In 'VB6 style' this would be something like:&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman';"&gt;Dim cmd As New SqlCommand( sql, connection )&lt;br /&gt;Dim rdr As SqlDataReader&lt;br /&gt;rdr = cmd.ExecuteReader()&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;VB 200x of course allows 1 less line:&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman';"&gt;Dim cmd As New SqlCommand( sql, connection )&lt;br /&gt;Dim rdr As SqlDataReader = cmd.ExecuteReader()&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman';"&gt;But the 2&lt;/span&gt;&lt;sup&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman';"&gt;nd&lt;/span&gt;&lt;/sup&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman';"&gt; line feels a little unnatural to a Java mindset. Type inference allows:&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman';"&gt;Dim cmd As New SqlCommand( sql, connection )&lt;br /&gt;Dim rdr = cmd.ExecuteReader()&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Which is now as concise as it would be in C#.  Type inference goes a bit further in VB than C#, for example:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman';"&gt;Dim someList As New List(Of SomeClass)&lt;br /&gt;For Each thing in someList&lt;br /&gt;           …&lt;br /&gt;Next thing&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;Where in C# the type of ‘thing’ needs to be given such as:&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman';"&gt;foreach( SomeClass thing in someList ) { … }&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;which is not very different but some reason many people find the &lt;/span&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;SomeClass thing&lt;/span&gt;&lt;/i&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt; confusing in training courses. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;As a last example, this is a trivial example of a lambda in C# which shows a message box (for something to do!) from a thread:&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman';"&gt;Thread t = new Thread( () =&gt; MessageBox.Show( "text " ) );&lt;br /&gt;t.Start();&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;The VB equivalent would be:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span class="Apple-style-span"  style="font-family:'times new roman';"&gt;Dim t As New Thread(Sub() MessageBox.Show("text"))&lt;br /&gt;t.Start()&lt;/span&gt;&lt;/p&gt;  &lt;span style="line-height: 115%; "&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;I feel this is a case where the VB is &lt;/span&gt;&lt;/span&gt;&lt;span style="line-height: 115%; "&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;more intuitive (and is actually less typing).&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;But that’s just me.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4464677400982090871-4155445880702079966?l=blog.tgn.co.uk' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog.tgn.co.uk/feeds/4155445880702079966/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4464677400982090871&amp;postID=4155445880702079966' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4464677400982090871/posts/default/4155445880702079966'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4464677400982090871/posts/default/4155445880702079966'/><link rel='alternate' type='text/html' href='http://blog.tgn.co.uk/2010/05/has-vb-come-of-age.html' title='Has VB come of age?'/><author><name>stevej</name><uri>http://www.blogger.com/profile/16907553839195552660</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4464677400982090871.post-7886981802307433868</id><published>2009-10-14T09:20:00.003+01:00</published><updated>2009-10-14T09:59:01.166+01:00</updated><title type='text'>WPF 3D, The Beauty of Mistakes</title><content type='html'>&lt;div&gt;When I first coded graphical applications for Windows using GDI many years ago, one thing I discovered was that coding mistakes could often produce very attractive results, particularly when using things like bitmaps and ROP modes. Not as exciting as some of my first work controlling industrial machines, when a mistake could cause 3 tonnes of metal cutting equipment to crash into its end-stops at high speed, but pretty nonetheless.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;With WPF 3D coding it's even better.  When writing code I like to test things out as often as possible, and this is particularly beneficial with 3D work as often when trying to visualize things in 3D space it's just easier to build things up gradually.  The sad thing is that the intermediate results often look better than the finished article, even if they are not correct.  For example, this is a chart before the size of the radial elements was calculated.&lt;/div&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.tgn.co.uk/blog/uploaded_images/flower-766940.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 303px; height: 320px;" src="http://www.tgn.co.uk/blog/uploaded_images/flower-766938.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;This is an incomplete spherical element.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.tgn.co.uk/blog/uploaded_images/spheremoreteeth-799856.png"&gt;&lt;/a&gt;&lt;div&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.tgn.co.uk/blog/uploaded_images/spheremoreteeth-799856.png" style="text-decoration: none;"&gt;&lt;img style="text-align: left;display: block; margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto; cursor: pointer; width: 320px; height: 179px; " src="http://www.tgn.co.uk/blog/uploaded_images/spheremoreteeth-799855.png" border="0" alt="" /&gt;&lt;/a&gt;And this is the same item with a little more coding.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.tgn.co.uk/blog/uploaded_images/sphereteeth-756304.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 305px;" src="http://www.tgn.co.uk/blog/uploaded_images/sphereteeth-756298.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Another example of this is the use of normals to influence lighting and shading. I most often build up the mesh by creating the points and triangle indices first, viewing the results for correct positioning etc.  While it's undoubtedly better to think about and hopefully calculate the normals at this time, I tend to do it afterwards.  I'm then disappointed because although the appearance is definitely more correct, it's often not as attractive.  The simple bar chart example below shows this.&lt;/div&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.tgn.co.uk/blog/uploaded_images/sharedtrianglebars-789299.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 299px; height: 300px;" src="http://www.tgn.co.uk/blog/uploaded_images/sharedtrianglebars-789297.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;div&gt;The finished article had much better defined edges and smooth colours, but it definitely didn't look as nice!&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4464677400982090871-7886981802307433868?l=blog.tgn.co.uk' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog.tgn.co.uk/feeds/7886981802307433868/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4464677400982090871&amp;postID=7886981802307433868' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4464677400982090871/posts/default/7886981802307433868'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4464677400982090871/posts/default/7886981802307433868'/><link rel='alternate' type='text/html' href='http://blog.tgn.co.uk/2009/10/wpf-3d-beauty-of-mistakes.html' title='WPF 3D, The Beauty of Mistakes'/><author><name>stevej</name><uri>http://www.blogger.com/profile/16907553839195552660</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4464677400982090871.post-87051749434036743</id><published>2009-10-08T08:24:00.000+01:00</published><updated>2009-10-08T09:57:26.042+01:00</updated><title type='text'>Silverlight and WPF</title><content type='html'>This week I have been doing some work producing charts. Many years ago I sold a product called 'Business Graphics Library' and since then many of my projects have involved special charts written on request for customers.  As an experiment I wanted to try porting a chart I called a 'starburst', which had originally been written in Excel VBA, to Silverlight.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The chart had a dynamic layout, so I wrote the new one entirely in C# rather than XAML.  As you can imagine it was much more straightforward than the Excel VBA original given the rich graphics supported by SL.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;img src="http://www.tgn.co.uk/starburstsl.jpeg" alt="Simple starburst" width="245" height="245" style="text-align:center; margin:auto;" /&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The next step was to use the same code for a WPF desktop application.  The Silverlight control included some data structures for passing the information to be plotted, including colours, and my first attempt was to use these, as well as the chart code itself, directly from the library by simply adding a reference to the Silverlight control DLL into the WPF project. This almost worked, but ultimately failed because the Silverlight classes I used were in different .Net assemblies to their WPF equivalents.  Even basic types like Color had this problem.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;To solve this I added the classes as existing items to the new project, but used the 'Add As Link' option in the dialog (by clicking the arrow on the Add button).  I now had common source between the 2 projects, and I could display my chart on a web page using Silverlight or a desktop app using WPF.  So far so good.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;At this point it was all driven from C#/VB code.  The next stage was to be able to use my control entirely from XAML, so some dependency properties were required.  Registering these using DependencyProperty.Register worked fine for WPF, which I did first, but I could not get the same code to compile for Silverlight.  It turns out that there are different Register methods, and the arguments I had used for WPF (which has 3 overloads) were not compatible with Silverlight (which has only one).  Luckily there is a WPF overload which is compatible with the Silverlight implementation so by passing a null for the 4th argument I had compatible code again.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Slightly disappointing to be entering the area of coding around differences.  Having a common API between desktop and web applications is a really good idea, and little differences like this seem to be pointless distractions.  Of course there are big differences as well.  Having used an image projection in SL I was disappointed not to find it in WPF.  But my real goal for the Starburst chart is a true 3D version (the subject of another post shortly), but this can only work for WPF as Silverlight does not support true 3D.  Given that Silverlight's one major advantage over Flash is the .Net Framework (IMHO) I would hate to seem them diverge.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4464677400982090871-87051749434036743?l=blog.tgn.co.uk' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog.tgn.co.uk/feeds/87051749434036743/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4464677400982090871&amp;postID=87051749434036743' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4464677400982090871/posts/default/87051749434036743'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4464677400982090871/posts/default/87051749434036743'/><link rel='alternate' type='text/html' href='http://blog.tgn.co.uk/2009/10/silverlight-and-wpf.html' title='Silverlight and WPF'/><author><name>stevej</name><uri>http://www.blogger.com/profile/16907553839195552660</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4464677400982090871.post-1013944353483688467</id><published>2008-06-05T15:21:00.000+01:00</published><updated>2008-06-05T15:23:23.878+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='performance'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Performance of C# vs C++</title><content type='html'>Many people see C# as a fine language for business applications, but feel it lacks the low-level capabilities of C++ for software which is close to the metal.&lt;br /&gt;&lt;br /&gt;C# includes many features such as the unsafe keyword that allows you to achieve most of what you could do in C++, but even without the use of unsafe code performance-intensive programming is possible. I was recently involved in a large project where I had the opportunity to compete with a C++ team, by writing an equivalent program from scratch in C#. Without wishing to break my NDA, the application involved much bit-twiddling, video, networking and the like. Originally I kept in an existing C++ library, but ended up using C# exclusively.&lt;br /&gt;&lt;br /&gt;The C# code was around 20% of the size of its C++ equivalent, measured using a sophisticated tool that ignored layout, comments etc. Performance ended up being about the same – there were initial performance problems with the C++ version which were fixed by some serious coding work.&lt;br /&gt;&lt;br /&gt;Where I expected the C# version to fall down was with load times, due to the just-in-time compilation. In fact this was very noticeably faster in the C# version – I presume that the JIT overhead was compensated for by the smaller code size.&lt;br /&gt;&lt;br /&gt;It would be nice to try something similar with Visual Basic one day!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4464677400982090871-1013944353483688467?l=blog.tgn.co.uk' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog.tgn.co.uk/feeds/1013944353483688467/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4464677400982090871&amp;postID=1013944353483688467' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4464677400982090871/posts/default/1013944353483688467'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4464677400982090871/posts/default/1013944353483688467'/><link rel='alternate' type='text/html' href='http://blog.tgn.co.uk/2008/06/performance-of-c-vs-c.html' title='Performance of C# vs C++'/><author><name>stevej</name><uri>http://www.blogger.com/profile/16907553839195552660</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4464677400982090871.post-7782563576050910962</id><published>2008-06-03T09:48:00.000+01:00</published><updated>2008-06-03T09:56:25.532+01:00</updated><title type='text'>Casting, 'is' and 'as' in C#</title><content type='html'>I was giving a course for a gaming company who were understandably interested in performance issues. One of the things discussed was the relative performance of traditional casting versus is and as, so I had a play.&lt;br /&gt;&lt;br /&gt;The general idea is that you have a reference variable of a base class type, which may be referring to an object of a derived class. This derived class has additional members which you wish to use, so you need to have a reference of the derived class type. For example:&lt;br /&gt;BaseClass b;&lt;br /&gt;...&lt;br /&gt;DerivedClass d = b;&lt;br /&gt;d.AMethodOnlyDerivedClassHas();&lt;br /&gt;&lt;br /&gt;And of course the second line above will be thrown out by the compiler as BaseClass is less than DerivedClass in hierarchy terms.&lt;br /&gt;&lt;br /&gt;A C-style cast could be used, for example:&lt;br /&gt;&lt;br /&gt;DerivedClass d = (DerivedClass)b;&lt;br /&gt;&lt;br /&gt;If you definitely know that b refers to a DerivedClass object then this is the fastest technique, about 5% faster that using ‘as’. But if b does not refer to a DerivedClass object (or an object of a class derived from it) then an exception is thrown, which is extremely expensive.&lt;br /&gt;When b may or may not refer to a DerivedClass object, ‘as’ is useful as it gives null when the object is not the expected class, for example:&lt;br /&gt;&lt;br /&gt;DerivedClass d = b as DerivedClass;&lt;br /&gt;if ( d != null )&lt;br /&gt;{&lt;br /&gt;d. AMethodOnlyDerivedClassHas();&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;This technique allows us to test and cast simultaneously. Although it is a bit slower if the object is the tested-for type, it is enormously faster (&gt;200 times) than catching the exception that the C-style cast throws.&lt;br /&gt;&lt;br /&gt;‘as’ is widely used because it allows us to combine the testing with the assignment to a reference of the correct type. ‘is’ can be used just to perform the test, for example:&lt;br /&gt;&lt;br /&gt;if ( b is DerivedClass )&lt;br /&gt;...&lt;br /&gt;And in fact ‘is’ and ‘as’ both use the same CLR code, so cost the same. Don’t do this though:&lt;br /&gt;&lt;br /&gt;if ( b is DerivedClass )&lt;br /&gt;{&lt;br /&gt;DerivedClass d = b as DerivedClass;&lt;br /&gt;d. AMethodOnlyDerivedClassHas();&lt;br /&gt;&lt;br /&gt;as you end up calling the same CLR support code twice.&lt;br /&gt;&lt;br /&gt;An alternative would be to compare types, for example:&lt;br /&gt;&lt;br /&gt;if ( b.GetType() == typeof( DerivedClass ) )&lt;br /&gt;&lt;br /&gt;This is about 10 times as slow as using ‘as’ or ‘is’, but allows an exact comparison rather than the ‘at least’ comparison used by ‘as’ and just about everything else. In practice this is probably not useful though.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4464677400982090871-7782563576050910962?l=blog.tgn.co.uk' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog.tgn.co.uk/feeds/7782563576050910962/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4464677400982090871&amp;postID=7782563576050910962' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4464677400982090871/posts/default/7782563576050910962'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4464677400982090871/posts/default/7782563576050910962'/><link rel='alternate' type='text/html' href='http://blog.tgn.co.uk/2008/06/casting-is-and-as-in-c.html' title='Casting, &apos;is&apos; and &apos;as&apos; in C#'/><author><name>stevej</name><uri>http://www.blogger.com/profile/16907553839195552660</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4464677400982090871.post-2725809248899712777</id><published>2008-06-03T08:38:00.000+01:00</published><updated>2008-06-03T08:52:13.391+01:00</updated><title type='text'>Getting Started</title><content type='html'>The idea is that this page will contain snippets I've gathered from my training and development work which will hopefully be useful to other developers.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4464677400982090871-2725809248899712777?l=blog.tgn.co.uk' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog.tgn.co.uk/feeds/2725809248899712777/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4464677400982090871&amp;postID=2725809248899712777' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4464677400982090871/posts/default/2725809248899712777'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4464677400982090871/posts/default/2725809248899712777'/><link rel='alternate' type='text/html' href='http://blog.tgn.co.uk/2008/06/getting-started.html' title='Getting Started'/><author><name>stevej</name><uri>http://www.blogger.com/profile/16907553839195552660</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
