• 0

Some dumb questions about C#


Question

Here are 5 classes  and their code. The entry point is set in program.cs and the class JewelThief inherits properties from Locksmith class .

class Jewels
    {
        public string Sparkle()
        {
            return "Sparkle, sparkle!";
        }
    }

 

class Safe
    {
        private Jewels contents = new Jewels();
        private string safeCombination = "12345";
        public Jewels Open(string combination)
        {
            if (combination == safeCombination)
                return contents;
            else
                return null;
        }
        public void PickLock(Locksmith lockpicker)
        {
            lockpicker.WriteDownCombination(safeCombination);
        }  
    }

 

 class Owner
    {
        private Jewels returnedContents;
        public void ReceiveContents (Jewels safeContents)
        {
            returnedContents = safeContents;
            Console.WriteLine("Thank you for returning my jewels!" + safeContents.Sparkle());
        }
    }

 

 class Locksmith
    {
        public void OpenSafe(Safe safe, Owner owner)
        {
            safe.PickLock(this);
            Jewels safeContents = safe.Open(writtenDownCombination);
            ReturnContents(safeContents, owner);
        }
        private string writtenDownCombination = null;
        public void WriteDownCombination(string combination)
        {
            writtenDownCombination = combination;
        }
        public void ReturnContents(Jewels safeContents, Owner owner)
        {
            owner.ReceiveContents(safeContents);
        }
    }

 

class JewelThief : Locksmith
    {
        private Jewels stolenJewels = null;
        public void ReturnContents(Jewels safeContents, Owner owner)
        {
            stolenJewels = safeContents;
            Console.WriteLine("I'm stealing the contents!" + stolenJewels.Sparkle());
        }
    }

 

class Program
    {
        static void Main(string[] args)
        {
            Owner owner = new Owner();
            Safe safe = new Safe();
            JewelThief jewelThief = new JewelThief();
            jewelThief.OpenSafe(safe, owner);
            Console.ReadKey();
        }
    }

 

As mentioned in program.cs the line jewelThief.OpenSafe(safe, owner); the output should be

 

"I'm stealing the contents! Sparkle, sparkle!" since the class Jewel.thief is not properly overrided  the output appears as  "Thank you for returning my jewels!, Sparkle, sparkle!" my question here is not about inheritance or overriding.

 

In the Class Safe.cs and Class Owner.cs there are variables declared in their methods with reference to other classes, they are as follows respectively :- 

 

public void PickLock(Locksmith lockpicker){...}      and      public void ReceiveContents (Jewels safeContents){...}

 

How are lockpicker and safeContents initialized and yet there is no error

 

Is this because of Visual studio C# which  makes a shortcut and let these lines compile ?

if so whats the use of declaring variables like

string lockpicker or string safeContents

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0
19 minutes ago, Ch33f said:

In the Class Safe.cs and Class Owner.cs there are variables declared in their methods with reference to other classes, they are as follows respectively :- 

 


public void PickLock(Locksmith lockpicker){...}      and      public void ReceiveContents (Jewels safeContents){...}

 

How are lockpicker and safeContents initialized and yet there is no error

Please use code formatting when you post code.

 

To answer your question I'll quote Eric Lippert:
 

Quote

The C# language does not require that declarations occur before usages (...) we have to have a “two pass” compiler. In the first pass, we look for declarations and ignore bodies. Once we have gleaned all the information from the declarations (...), we take a second pass over the code and generate the IL for the bodies.

How are lockpicker and safeContents initialized and yet there is no error? In the lines you mention, nothing is initialized; at compile-time, the compiler simply notes that there is a method called PickLock that takes a Locksmith in argument, and it doesn't need even need to know yet what is a Locksmith, it just makes a note and resolves that later when it encounters the Locksmith class. If the Locksmith class doesn't exist anywhere, then you'll get an error.

 

At run-time, nothing happens on that line either (notice you can't put a breakpoint on it): it's purely a declaration, i.e. information for you and the compiler. The body of the method is the executable code, where run-time things happen, such as initializations, assignments, method calls, etc.

 

Make sure you understand these differences between compile and run time, and between declarations and bodies.

Link to comment
Share on other sites

  • 0
48 minutes ago, Andre S. said:

Please use code formatting when you post code.

 

To answer your question I'll quote Eric Lippert:
 

How are lockpicker and safeContents initialized and yet there is no error? In the lines you mention, nothing is initialized; at compile-time, the compiler simply notes that there is a method called PickLock that takes a Locksmith in argument, and it doesn't need even need to know yet what is a Locksmith, it just makes a note and resolves that later when it encounters the Locksmith class. If the Locksmith class doesn't exist anywhere, then you'll get an error.

 

At run-time, nothing happens on that line either (notice you can't put a breakpoint on it): it's purely a declaration, i.e. information for you and the compiler. The body of the method is the executable code, where run-time things happen, such as initializations, assignments, method calls, etc.

 

Make sure you understand these differences between compile and run time, and between declarations and bodies.

 thank you this was very helpful, i'll remember to use code formatting next time i post any code

 

Link to comment
Share on other sites

This topic is now closed to further replies.