<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Granville Barnett : C++/CLI</title><link>http://weblogs.asp.net/gbarnett/archive/tags/C_2B002B002F00_CLI/default.aspx</link><description>Tags: C++/CLI</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>STL/CLR - a fairly low key, but awesome new addition in .NET 3.5</title><link>http://weblogs.asp.net/gbarnett/archive/2007/10/27/stl-clr-a-fairly-low-key-but-awesome-new-addition-in-net-3-5.aspx</link><pubDate>Sat, 27 Oct 2007 18:59:07 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4785149</guid><dc:creator>gbarnett</dc:creator><author>gbarnett</author><slash:comments>8</slash:comments><description>&lt;p&gt;I really like what the C++ team have done here, they've essentially created a subset of the stuff found in the C++ STL but included&amp;nbsp;error checking and stuff like that - of course this all requires .NET 3.5.&lt;/p&gt; &lt;p&gt;It seems that the STL/CLR stuff has been a victim of the more cooler features in the app languages C# and VB.NET like LINQ, and err... LINQ.&lt;/p&gt; &lt;p&gt;Check it out here - &lt;a title="http://msdn2.microsoft.com/en-us/library/bb385954(VS.90).aspx" href="http://msdn2.microsoft.com/en-us/library/bb385954(VS.90).aspx"&gt;http://msdn2.microsoft.com/en-us/library/bb385954(VS.90).aspx&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;I believe that all the containers act like their C++ STL counterparts from an interaction perspective, with a few under the hood error checking things thrown in to make them "safer".&amp;nbsp; I mean&amp;nbsp;where&amp;nbsp;would you be without the trusty vector container?! :-)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4785149" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gbarnett/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gbarnett/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://weblogs.asp.net/gbarnett/archive/tags/C_2B002B002F00_CLI/default.aspx">C++/CLI</category></item><item><title>MSIL:  Part 4</title><link>http://weblogs.asp.net/gbarnett/archive/2007/09/07/msil-part-4.aspx</link><pubDate>Fri, 07 Sep 2007 18:39:11 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:3781251</guid><dc:creator>gbarnett</dc:creator><author>gbarnett</author><slash:comments>1</slash:comments><description>&lt;p&gt;In this part we will look at the differences between what the C++/CLI compiler generates and that the C# compiler generates with regards to MSIL.&lt;/p&gt; &lt;p&gt;We won't be creating any MSIL applications this time, but rather we will be analyzing the MSIL.&lt;/p&gt; &lt;h4&gt;C++/CLI&lt;/h4&gt; &lt;p&gt;I really like C++/CLI, it brings the best of both worlds - managed and unmanaged and allows the developer to jump in and out of each world seamlessly.&amp;nbsp; I would like to point out that C++/CLI and C++ (and C) are more tailored towards system development rather than application development which are more C# and VB.NET space.&lt;/p&gt; &lt;p&gt;&lt;em&gt;Note:&amp;nbsp; the next version of C++ will include a garbage collector that will be turned off by default.&lt;/em&gt;&lt;/p&gt; &lt;p&gt;For this part I'm going to use two examples, a Person class that will be the same in C# as C++ although in C++ I will use get/set methods instead of properties (although properties are get_ and set_ methods after the compiler has worked it's magic).&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Person.h&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;#pragma once

ref &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Person
{
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;:
	Person(&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;);
	Person(System::String^ firstName, System::String^ lastName);
	System::String^ GetFirstName(&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;);
	&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SetFirstName(System::String^ firstName);
	System::String^ GetLastName(&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;);
	&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SetLastName(System::String^ lastName);
	&lt;span style="color: #0000ff"&gt;virtual&lt;/span&gt; System::String^ ToString(&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;) override;
&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;:
	System::String^ m_firstName;
	System::String^ m_lastName;
};
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Person.cpp&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;#include "&lt;span style="color: #8b0000"&gt;StdAfx.h&lt;/span&gt;"
#include "&lt;span style="color: #8b0000"&gt;Person.h&lt;/span&gt;"

&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; &lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt; System;

Person::Person(&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;)
{
	m_firstName = System::String::Empty;
	m_lastName = System::String::Empty;
}

Person::Person(System::String^ firstName, System::String^ lastName) 
{
	m_firstName = firstName;
	m_lastName = lastName;
}

System::String^ Person::GetFirstName(&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;)
{
	&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; m_firstName;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Person::SetFirstName(System::String^ firstName)
{
	m_firstName = firstName;
}

System::String^ Person::GetLastName(&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;) 
{
	&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; m_lastName;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Person::SetLastName(System::String^ lastName)
{
	m_lastName = lastName;
}

System::String^ Person::ToString(&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;)
{
	&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; m_firstName + "&lt;span style="color: #8b0000"&gt; &lt;/span&gt;" + m_lastName;
}&lt;/pre&gt;
&lt;p&gt;We will create a similar Person class in C# now then compare the MSIL generated for a few methods.&lt;/p&gt;
&lt;h4&gt;C#&lt;/h4&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System;

&lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt; MsilTest
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Person
    {
        &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; _firstName;
        &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; _lastName;

        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Person(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; firstName, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; lastName)
        {

            _firstName = firstName;
            _lastName = lastName;
        }

        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; FirstName
        {
            &lt;span style="color: #0000ff"&gt;get&lt;/span&gt; { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _firstName; }
            &lt;span style="color: #0000ff"&gt;set&lt;/span&gt; { _firstName = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;; }
        }

        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; LastName
        {
            &lt;span style="color: #0000ff"&gt;get&lt;/span&gt; { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _lastName; }
            &lt;span style="color: #0000ff"&gt;set&lt;/span&gt; { _lastName = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;; }
        }

        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; ToString()
        {
            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _firstName + "&lt;span style="color: #8b0000"&gt; &lt;/span&gt;" + _lastName;
        }

    }
}&lt;/pre&gt;
&lt;h4&gt;The comparison&lt;/h4&gt;
&lt;p&gt;We will look at the MSIL generated first for the ToString() method.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;C++/CLI&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;.&lt;span style="color: #0000ff"&gt;method&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;hidebysig&lt;/span&gt; virtual &lt;span style="color: #0000ff"&gt;instance&lt;/span&gt; string 
        ToString() &lt;span style="color: #0000ff"&gt;cil&lt;/span&gt; &lt;span style="color: #0000ff"&gt;managed&lt;/span&gt;
{
  &lt;span style="color: #008000"&gt;// Code size       30 (0x1e)&lt;/span&gt;
  .maxstack  2
  .locals ([0] string V_0)
  IL_0000:  &lt;span style="color: #0000ff"&gt;ldarg&lt;/span&gt;.0
  IL_0001:  &lt;span style="color: #0000ff"&gt;ldfld&lt;/span&gt;      string Person::m_firstName
  IL_0006:  &lt;span style="color: #0000ff"&gt;ldstr&lt;/span&gt;      "&lt;span style="color: #8b0000"&gt; &lt;/span&gt;"
  IL_000b:  &lt;span style="color: #0000ff"&gt;call&lt;/span&gt;       string [mscorlib]System.String::Concat(string,
                                                              string)
  IL_0010:  &lt;span style="color: #0000ff"&gt;ldarg&lt;/span&gt;.0
  IL_0011:  &lt;span style="color: #0000ff"&gt;ldfld&lt;/span&gt;      string Person::m_lastName
  IL_0016:  &lt;span style="color: #0000ff"&gt;call&lt;/span&gt;       string [mscorlib]System.String::Concat(string,
                                                              string)
  IL_001b:  stloc.0
  IL_001c:  &lt;span style="color: #0000ff"&gt;ldloc&lt;/span&gt;.0
  IL_001d:  &lt;span style="color: #0000ff"&gt;ret&lt;/span&gt;
} &lt;span style="color: #008000"&gt;// end of method Person::ToString&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;.&lt;span style="color: #0000ff"&gt;method&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;hidebysig&lt;/span&gt; virtual &lt;span style="color: #0000ff"&gt;instance&lt;/span&gt; string 
        ToString() &lt;span style="color: #0000ff"&gt;cil&lt;/span&gt; &lt;span style="color: #0000ff"&gt;managed&lt;/span&gt;
{
  &lt;span style="color: #008000"&gt;// Code size       28 (0x1c)&lt;/span&gt;
  .maxstack  3
  .locals &lt;span style="color: #0000ff"&gt;init&lt;/span&gt; ([0] string CS$1$0000)
  IL_0000:  &lt;span style="color: #0000ff"&gt;nop&lt;/span&gt;
  IL_0001:  &lt;span style="color: #0000ff"&gt;ldarg&lt;/span&gt;.0
  IL_0002:  &lt;span style="color: #0000ff"&gt;ldfld&lt;/span&gt;      string MsilTest.Person::_firstName
  IL_0007:  &lt;span style="color: #0000ff"&gt;ldstr&lt;/span&gt;      "&lt;span style="color: #8b0000"&gt; &lt;/span&gt;"
  IL_000c:  &lt;span style="color: #0000ff"&gt;ldarg&lt;/span&gt;.0
  IL_000d:  &lt;span style="color: #0000ff"&gt;ldfld&lt;/span&gt;      string MsilTest.Person::_lastName
  IL_0012:  &lt;span style="color: #0000ff"&gt;call&lt;/span&gt;       string [mscorlib]System.String::Concat(string,
                                                              string,
                                                              string)
  IL_0017:  stloc.0
  IL_0018:  &lt;span style="color: #0000ff"&gt;br&lt;/span&gt;.s       IL_001a
  IL_001a:  &lt;span style="color: #0000ff"&gt;ldloc&lt;/span&gt;.0
  IL_001b:  &lt;span style="color: #0000ff"&gt;ret&lt;/span&gt;
} &lt;span style="color: #008000"&gt;// end of method Person::ToString&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;There's not much difference at all between the C++/CLI and C# implementation&amp;nbsp;- the C# compiler tells the CLR it wants 3 slots on the stack max because it uses the Concat method which takes 3 args thus 3 strings are pushed onto the stack prior to the method call where as the C++/CLI implementation calls the Concat method twice using the 2 arg version, hence the maxstack being 2.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note:&amp;nbsp; both binaries are debug releases and are not optimized.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In the C# MSIL there is a nop instruction this is used for debugging, e.g. breakpoints.&lt;/p&gt;
&lt;p&gt;This post is getting a little big so I will follow up this post in the next partof the MSIL series.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=3781251" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gbarnett/archive/tags/MSIL/default.aspx">MSIL</category><category domain="http://weblogs.asp.net/gbarnett/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gbarnett/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/gbarnett/archive/tags/C_2B002B002F00_CLI/default.aspx">C++/CLI</category></item><item><title>DSA 0.1 released</title><link>http://weblogs.asp.net/gbarnett/archive/2007/09/07/dsa-0-1-released.aspx</link><pubDate>Fri, 07 Sep 2007 10:37:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:3777256</guid><dc:creator>gbarnett</dc:creator><author>gbarnett</author><slash:comments>0</slash:comments><description>&lt;P&gt;Today, I pushed the release button on a little project I've been doing over on &lt;A href="http://www.codeplex.com/" mce_href="http://www.codeplex.com/"&gt;CodePlex&lt;/A&gt; entitled &lt;A href="http://www.codeplex.com/dsa" mce_href="http://www.codeplex.com/dsa"&gt;Data Structures and Algorithms (DSA)&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The story of the project is to basically go ahead and without looking at the .NET source create custom implementations of some of the stuff in the &lt;A href="http://msdn2.microsoft.com/en-us/library/system.collections.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.collections.aspx"&gt;System.Collections&lt;/A&gt; namespace (as well as some collections that aren't present), as well as providing algorithms for common problems - these will mainly be implemented as extension methods which were introduced in C# 3.0 to give that smooth integration with the .NET stack.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.codeplex.com/dsa/Release/ProjectReleases.aspx?ReleaseId=6683" mce_href="http://www.codeplex.com/dsa/Release/ProjectReleases.aspx?ReleaseId=6683"&gt;Data Structures and Algorithms (DSA) 0.1&lt;/A&gt;, includes:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;SinglyLinkedListCollection(Of T) 
&lt;LI&gt;QueueCollection(Of T) 
&lt;LI&gt;StackCollection(Of T)&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;As well as those data structures, this release has:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;VS debugging integration 
&lt;LI&gt;LINQ support 
&lt;LI&gt;Act like .NET collections (implement all major interfaces) 
&lt;LI&gt;CLS compliant (C# and C++/CLI examples for download) 
&lt;LI&gt;Full FxCop compliance&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Go &lt;A href="http://www.codeplex.com/dsa/Release/ProjectReleases.aspx?ReleaseId=6683" mce_href="http://www.codeplex.com/dsa/Release/ProjectReleases.aspx?ReleaseId=6683"&gt;download&lt;/A&gt; it, tell me what you think and where I have gone wrong or right!&amp;nbsp;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=3777256" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gbarnett/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gbarnett/archive/tags/Data+Structures/default.aspx">Data Structures</category><category domain="http://weblogs.asp.net/gbarnett/archive/tags/Algorithms/default.aspx">Algorithms</category><category domain="http://weblogs.asp.net/gbarnett/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/gbarnett/archive/tags/C_2B002B002F00_CLI/default.aspx">C++/CLI</category><category domain="http://weblogs.asp.net/gbarnett/archive/tags/DSA/default.aspx">DSA</category></item></channel></rss>