In the code below, I'M having trouble with one line of code

MenuMaker menu = new MenuMaker() { Randomizer = new Random() };

The program is a simple windows Form with two main files, Form1.cs & MenuMaker.cs, the second one is a class.

What I'M trying to understand is why is the line of code above in the Form1.cs file instead of the MenuMaker.cs class where the rest of my fields are at, including the creation of my Random instance. The two files are below.

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DiscountSandwiches
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            MenuMaker menu = new MenuMaker() { Randomizer = new Random() };
                
            label1.Text = menu.GetMenuItem();
            label2.Text = menu.GetMenuItem();
            label3.Text = menu.GetMenuItem();
            label4.Text = menu.GetMenuItem();
            label5.Text = menu.GetMenuItem();
            label6.Text = menu.GetMenuItem();
        }
    }
}

MenuMaker.cs

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

namespace DiscountSandwiches
{
    public class MenuMaker
    {
        // fields
        public Random Randomizer;


        string[] Meats = { "Roast beef", "Salami", "Turkey", "Ham", "Pastrami" };
        string[] Condiments = { "yellow mustard", "brown mustard", "honey mustard",
                                  "mayo", "relish", "french dressing" };
        string[] Breads = { "rye", "white", "wheat", "pumpernickel", "itelian bread", "a roll" };

        // methods
        public string GetMenuItem()
        {
            string randomMeat = Meats[Randomizer.Next(Meats.Length)];
            string randomCondiment = Condiments[Randomizer.Next(Condiments.Length)];
            string randomBread = Breads[Randomizer.Next(Breads.Length)];

            return randomMeat + " with " + randomCondiment + " on " + randomBread;
        }

    }
}
MenuMaker menu = new MenuMaker() { Randomizer = new Random() };

That's C# 3.0's object initializer feature. New in 3.0, you are able to set public properties at class instantiation time. It would be the same as writing this:

MenuMaker menu = new MenuMaker();
menu.Randomizer = new Random();

In 3.0, you're simply allowed to inline those property initializations if you desire. You could do the same thing with other properties or fields, provided that they were publicly visible.

Now, if you wanted to encapsulate the logic of the MenuMaker class (and hide the fact that it was using an instance of Random to give you your menu items), then you'd want to consider making Randomizer private, and then you would create a public constructor in MenuMaker that would initializer Randomizer.

public class MenuMaker
{
     public MenuMaker()
     {
            Randomizer = new Random();
     }

     private Random Randomizer;
     // ...

And then you would remove the object initializers from the MenuMaker instantiation in your form1 code.

MenuMaker menu = new MenuMaker(); // no initializers included
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.