18
Answers

NP143 struct

Ask a question
Maha

Maha

16y
3.4k
1

NP143 struct

 

http://www.c-sharpcorner.com/UploadFile/rmcochran/chsarp_memory401152006094206AM/chsarp_memory4.aspx

 

In the above website following program is given. What is the meaning of Bill.LeftShoe = new Shoe(); & Bill.RightShoe = new Shoe(); highlighted in yellow in the program.

 

Please explain the meaning.

 

Thank you

 

using System;

 

public struct Shoe

{

    public string Color;

}

 

public class Dude

{

    public string Name;

    public Shoe RightShoe;

    public Shoe LeftShoe;

 

    /*

    public Dude CopyDude()

    {

        Dude newPerson = new Dude();

 

        newPerson.Name = Name;

        newPerson.LeftShoe = LeftShoe;

        newPerson.RightShoe = RightShoe;

 

        return newPerson;

    }

    */

 

    public override string ToString()

    {

        return (Name + " : Dude!, I have a "

        + RightShoe.Color + " shoe on my right foot, and a " + LeftShoe.Color + " on my left foot.");

    }

}

class x

{

    public static void Main()

    {

        Dude Bill = new Dude();

 

        Bill.Name = "Bill";

        Bill.LeftShoe.Color = Bill.RightShoe.Color = "Blue";

 

        //Bill.LeftShoe = new Shoe();

        //Bill.RightShoe = new Shoe();

       

        //Dude Ted = Bill.CopyDude();

                  //OR

        Dude Ted = new Dude();

        //Bill.CopyDude();

 

        Ted.Name = "Ted";

        Ted.LeftShoe.Color = Ted.RightShoe.Color = "Red";

       

        Console.WriteLine(Bill.ToString());

        Console.WriteLine(Ted.ToString());

    }

}

/*

Bill : Dude!, I have a Blue shoe on my right foot, and a Blue on my left foot.

Ted : Dude!, I have a Red shoe on my right foot, and a Red on my left foot.

*/


Answers (18)