Luke Hoban's Blog

June, 2007

  • LukeH's WebLog

    .NET Framework Multitargeting in Visual Studio 2008 (aka Orcas)

    • 33 Comments

    One of the really great features I worked on for our upcoming release is ".NET Framework Multitargeting" for Visual Studio.  This allows you to build applications targeting any of these frameworks using Visual Studio 2008:

    • .NET Framework 2.0 - released with Visual Studio 2005
    • .NET Framework 3.0 - released with Windows Vista
    • .NET Framework 3.5 - will release with Visual Studio Orcas

    When targeting .NET Framework 2.0, for example, Visual Studio will try to protect you from using any features that are only available in a higher framework version.  This lets you confidently use Visual Studio 2008 for targeting any of these three platforms.  New projects can be created targeting any of these frameworks, and projects can later be changed to target a different framework.

    Why is this so important?

    We've heard over-and-over again from .NET developers about how much harder it is to move to the next framework version than to move to the next Visual Studio version.  Upgrading to a new Visual Studio means installing on a few developer machines - upgrading to target a new framework means ensuring that every client of the application has the new framework installed.  Because of this, we very often get asked about whether it's possible to target the older frameworks with the newer tools.  With Visual Studio 2008 - we're enabling just that.

    There's another reason this is important for Visual Studio 2008 in particular.  With .NET 2.0, .NET 3.0 and .NET 3.5, we'll have released three framework versions in about 2 years - but only two Visual Studio versions.  Because of this - Visual Studio 2008 needs to be a great tool for targeting both .NET3.0 and .NET3.5, and multitrargeting enables this.  In addition, we want developers and development organizations to be able to easily move from Visual Studio 2005 to Visual Studio 2008 - and support for multitargeting lowers the barrier to entry - you can continue working on your .NET 2.0 applications in Visual Studio 2008.

    A few details...

    New Project Dialog

    The New Project and New Website dialogs now have an additional UI element to choose the framework version to target.  Choosing a target will limit the set of available templates in the dialog.  Additionally, templates that are available for multiple targets will be generated with different default references and usings/imports based on the framework target.  Out of the box, the defaults are to target .NET3.5 - but, just like other options in the dialog, the choice is "sticky", so if you pick .NET2.0, it'll default to that in the future.

    Since some templates are unaffected by the .NET Framework target - the description of the template also mentions whether or not a specific .NET Framework target will be applied.

    Project Properties

    You can change the framework target of your project at any time through the Project->Properties dialog.  If you change the target to a higher framework version, you will be able to use the new API features available in that framework.  If you change the target to a lower framework version, any references in your project which are no longer allowed will be disabled (but left in your References list), and compiling your project will likely result in build failures due to the missing assemblies.  However, we've tried to ensure that changing to a lower framework is easily reversible.

    In C# projects, the drop-down to make these changes appears on the Application tab, for VB projects it appears in the Compiler->Advanced... tab and for C++ projects it appears in the Framework and References section. 

    Add Reference Dialog

    The real heart of multitargeting support is the Add Reference dialog (see "So, how does this work?" below for details).

    In the ".NET" tab, references which are not available for the target framework are grayed out and cannot be added.  A hover-tip message explains the reason why.  All the assemblies that ship as part of the higher frameworks will be grayed out, as will any other assemblies which themselves have references to assemblies in the higher frameworks.

    In the "Browse" tab, you can still browse to any assembly, but if that assembly requires a higher framework version than your project, a warning dialog will be presented.

    Add New Item Dialog

    Some of the items that can be added to a project are dependent on a minimum framework version.  For example the "Linq to SQL Classes" item template requires .NET 3.5.  The Add New Item dialog thus filters out these items that are not available for the project.  Custom or third party templates can also set the framework versions they want to be available in - so that they can integrate correctly into this filtering.

    Toolbox

    The toolbox provides controls from many different assemblies - some of which may not be available for the framework target of your project.  So any controls defined in assemblies which are not available on your target framework will be filtered out of the toolbox.  For example, the ElementHost WinForms control which allows WPF interop as part of .NET3.0 is not available in the toolbox when building a WinForms project targeting .NET 2.0.

    Deployment

    At the end of the day, .NET framework target is all about deployment prequisites.  So in the Prerequisites dialog we've provided a few options for which .NET Framework package to include with your deployment.

    Language

    Many of the new language features in the .NET languages can be used with any of the available .NET framework targets.  So Visual Studio 2008 allows you to use C#3.0, VB9, and VC9 for all projects, including those targeting .NET2.0 and .NET3.0.

    Websites are different in this regard - you cannot use the new language features in a web site targeting .NET 2.0 or .NET 3.0.  This restriction is in place because Web Sites need to compile on the web server, and this requires that the new compilers be installed on the Web Server.  Since these new compilers come as part of the .NET 3.5 framework, this framework is required on the web server to use new language features.

    Express

    The Visual Studio 2008 Express products have a limited version of multitargeting.  All of the infrastructure described above is available - except for the dropdown in the New Project Dialog.  This keeps the multitargeting features out of the way for beginning and hobbyist developers - but ensures that projects can continue to move between Express and full versions of Visual Studio as they could in Visual Studio 2005.

     

    So, how does this work?

    The three frameworks supported by Visual Studio Orcas are each supersets of one another - instead of being replacements as the previous .NET Framework releases have been.  One benefit of this is that .NET Framework Multitargeting can be based just on the available set of references.  That is - when you target .NET3.0, the only things you should be prevented from using are those that depend on assemblies in .NET3.5 - and this can be done by ensuring that you don't accidentally reference these assemblies.

    There is one caveat to this - called "red bits".  These are the changes in the framework that are part of .NET Framework 2.0 Service Pack 1 and .NET Framework 3.0 Service Pack 1, both of which will be released along with Visual Studio 2008.  These two service packs are required for .NET3.5 to be installed, and they include a few very targetted additions of new functionality to the .NET 2.0 and .NET 3.0 frameworks.  This means that when you target .NET 2.0 in Visual Studio Orcas, you are really targeting .NET 2.0 SP1.  This is similar to what happens when you are using Visual Studio 2003 after the .NET Framework 1.1 service pack is installed on your machine - so it's really nothing new.  But for those who want to be extra cautious about their framework dependencies, it's somthing to be aware of. 

    Try it Now!

    Orcas Beta1 is available now, and includes almost all of the multitargeting support described above.  Take a look and let us know what you think.  Orcas Beta2, which will be available later this summer, will include everything described here - along with a ton of other improvements and bug fixes across the rest of Visual Studio 2008 and the .NET 3.5 Framework.  As always, send us feedback through Connect.

  • LukeH's WebLog

    C#3.0 Session at TechEd2007 - Code Samples

    • 1 Comments

    I just presented my C#3.0 session at TechEd Orlando titled "DEV346 - Microsoft Visual C# Under the Covers: An In-Depth Look at C# 3.0".  The talk introduces the new C# language features and takes a tour behind the scenes of LINQ to Objects to see how C# is used to enable creating rich new kinds of APIs.

    Here are the code samples from the talk:

    File iconFirstDemo.cs     File iconSecondDemo.cs

    And here's a little in-line snippet - the results of the first demo:

    using System;
    using System.Collections.Generic;
    using System.Linq;

    class Customer
    {
        // C#3.0 Feature:  Auto-Implemented Properties
        public string CustomerID { get; set; }
        public string ContactName { get; set; }
        public string City { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = LoadCustomers();
            
            // C#3.0 Feature:  Query Expressions
            var query = from c in customers
                             where c.City == "London"
                             orderby c.ContactName.Split(' ')[1]
                             select new { c.CustomerID, c.ContactName };

            foreach (var item in query)
                Console.WriteLine("{0}, {1}", item.CustomerID, item.ContactName);
        }

        private static List<Customer> LoadCustomers()
        {
            // C#3.0 Feature:  'var' - Local Variable Type Inference
            // C#3.0 Feature:  Collection Initializers
            var customers = new List<Customer>()
            {
                // C#3.0 Feature:  Object Initializers
                new Customer { CustomerID = "ALFKI",
                                       ContactName = "Maria Anders",
                                       City = "Berlin"},
                new Customer { CustomerID = "ANATR",
                                       ContactName = "Ana Trujillo",
                                       City = "M?xico D.F."},
                // more customers ...
                new Customer { CustomerID = "WOLZA",
                                       ContactName = "Zbyszek Piestrzeniewicz",
                                       City = "Warszawa"}
            };

            return customers;
        }
    }

  • LukeH's WebLog

    F12 - Go To Definition

    • 17 Comments

    A few months ago when Bill Gates was on the Daily Show, Jon Stewart asked him a particularly insightful software question:

    "So...what does the F12 button do?  Does it do anything?  Is it a joke button".  To which Bill replied jokingly - "I'd stay away from it if I were you." 

    If you use Visual Studio though - I recommend you ignore Bill's advice here.  F12 is a quite useful keyboard shortcut.

    Go To Definition

    F12 itself invokes the "Go To Definition" (GTD) command.  This works on types, members, locals, range variables, etc.  It uses all the compiler's internal understanding of the code to make sure it takes you to the right member - even when there are many overloads or if your code is using a lot of type inference.

    Metadata As Source

    One of the additions to "Go To Definition" support in Visual Studio 2005 was something we call "Metadata as Source".  If you go to definition on a type which is defined in a referenced assembly - you will be presented with a "header file"-style view of the corresponding type and it's members.  This keeps you in the editor - and gives a convenient view of the type.  You can continue to navigate other members/types directly from the Metadata As Source view by using GTD again on those tokens.

    Backward and Forward navigation

    If you use GTD a lot, you'll probably find yourself wanting to go "back" to where you invoked "go to definition" from.  It turns out there is a keybinding for this:

    • ctrl-shift-8: Takes you back in the GTD stack, to the the last place you invoked GTD
    • ctrl-shift-7:  Takes you forward in the GTD stack, to the last place you invoked back
    Code Definition Window

    The Code Definition Window is toolwindow which shows a live GTD of whatever symbol is currently under the cursor in the editor.  If you find yourself using GTD a lot, you may want to pop up the Code Definition Window with ctrl-w-d.

    BTW - If you are looking for more useful keyboard shortcuts using the C# Profile in Visual Studio - take a look at  the C# keybinding posters Karen blogged about a little while ago.

  • LukeH's WebLog

    At TechEd Orlando

    • 4 Comments

    I just arrived in Orlando for TechEd 2007.  I'll be presenting later on in the week on C#3.0 - but untill then, I get to spend some time talking to folks at the C# booth and attending a couple of the C#, LINQ and Visual Studio sessions to see what people are saying.

    If you are at TechEd this year - come by some of our C# and LINQ sessions - starting with the LINQ Overview talk Tuesday at 8:30AM.  And definitely stop by the C# booth to say "Hi"!

    If you aren't at TechEd this year - there's actually a number interesting public live webcasts of TechEd presentations which will be available at Vistual TechEd.  In particualr, the LINQ Overview presentaiton will be WebCast live.

Page 1 of 1 (4 items)