Choose Element (MSBuild)

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

Evaluates child elements to select one set of ItemGroup elements and/or PropertyGroup elements to evaluate.

<Project>
<Choose>
<When>
<Choose>
...
<Otherwise>
<Choose>
...

<Choose>  
    <When Condition="'StringA'=='StringB'">... </When>  
    <Otherwise>... </Otherwise>  
</Choose>  

The following sections describe attributes, child elements, and parent elements.

Attributes

None.

Child Elements

ElementDescription
OtherwiseOptional element.

Specifies the block of code PropertyGroup and ItemGroup elements to evaluate if the conditions of all When elements evaluate to false. There may be zero or one Otherwise elements in a Choose element, and it must be the last element.
WhenRequired element.

Specifies a possible block of code for the Choose element to select. There may be one or more When elements in a Choose element.

Parent Elements

ElementDescription
OtherwiseSpecifies the block of code to execute if the conditions of all When elements evaluate to false.
ProjectRequired root element of an MSBuild project file.
WhenSpecifies a possible block of code for the Choose element to select.

The Choose, When, and Otherwise elements are used together to provide a way to select one section of code to execute out of a number of possible alternatives. For more information, see Conditional Constructs.

The following project uses the Choose element to select which set of property values in the When elements to set. If the Condition attributes of both When elements evaluate to false, the property values in the Otherwise element are set.

<Project  
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >  
    <PropertyGroup>  
        <Configuration Condition="'$(Configuration)' == ''">Debug</Configuration>  
        <OutputType>Exe</OutputType>  
        <RootNamespace>ConsoleApplication1</RootNamespace>  
        <AssemblyName>ConsoleApplication1</AssemblyName>  
        <WarningLevel>4</WarningLevel>  
    </PropertyGroup>  
    <Choose>  
        <When Condition=" '$(Configuration)'=='debug' ">  
            <PropertyGroup>  
                <DebugSymbols>true</DebugSymbols>  
                <DebugType>full</DebugType>  
                <Optimize>false</Optimize>  
                <OutputPath>.\bin\Debug\</OutputPath>  
                <DefineConstants>DEBUG;TRACE</DefineConstants>  
            </PropertyGroup>  
            <ItemGroup>  
                <Compile Include="UnitTesting\*.cs" />  
                <Reference Include="NUnit.dll" />  
            </ItemGroup>  
        </When>  
        <When Condition=" '$(Configuration)'=='retail' ">  
            <PropertyGroup>  
                <DebugSymbols>false</DebugSymbols>  
                <Optimize>true</Optimize>  
                <OutputPath>.\bin\Release\</OutputPath>  
                <DefineConstants>TRACE</DefineConstants>  
            </PropertyGroup>  
        </When>  
        <Otherwise>  
            <PropertyGroup>  
                <DebugSymbols>true</DebugSymbols>  
                <Optimize>false</Optimize>  
                <OutputPath>.\bin\$(Configuration)\</OutputPath>  
                <DefineConstants>DEBUG;TRACE</DefineConstants>  
            </PropertyGroup>  
        </Otherwise>  
    </Choose>  
    <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />  
</Project>  

Conditional Constructs
Project File Schema Reference

Show: