計算機科学のブログ

types and references - Getting the reference - Random, Array, index

Head First C#: A Learner’s Guide to Real-World Programming with C# and .NET Core (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)のChapter 4(types and references - Getting the reference)、p.721(Exercise)の解答を求めてみる。

コード

index.razor

@page "/"

<div class="container">
    @foreach (MenuItem menuItem in menuItems)
    {
        <div class="row">
            <div class="col-9">
                @menuItem.Description
            </div>
            <div class="col-3">
                @menuItem.Price
            </div>
        </div>
    }
    <div class="row justify-content-center">
        <div class="col-6">
            <strong>Add guacamole for @guacamolePrice</strong>
        </div>
    </div>
</div>
@code {
    MenuItem[] menuItems = new MenuItem[5];
    string guacamolePrice;

    protected override void OnInitialized()
    {
        for (int i = 0; i < 5; i++)
        {
            menuItems[i] = new MenuItem();
            if (i > 2)
            {
                menuItems[i].Breads = new string[]
                {
                    "plain bagel",
                    "onion bagel",
                    "pumpernickel bagel",
                    "everyuthing bagel"
                };
            }
            menuItems[i].Generate();
        }
        MenuItem menuItem = new MenuItem();
        menuItem.Generate();
        guacamolePrice = menuItem.Price;
    }
}

Discount Sandwiches