W
W
wrewolf2013-06-28 17:46:22
C++ / C#
wrewolf, 2013-06-28 17:46:22

Question about delegating constructor c#?

How will this code work?
If 1 argument is received in the constructor, will an object be created with 2 default values?
If 2 then with only 1 default value.
I understand correctly?

    class Block
    {
        private int id;
        private int meta;
        private stringname;
        private float breakTime;
 
        public Block(int id)
            : this(id, 0) { }
 
        public Block(int id, int meta)
            : this(id, meta, "Unknown") { }
 
        public Block(int id, int meta, string name)
        {
            this.id = (int)id;
            this.meta = (int)meta;
            this.name = name;
            breakTime = 0.20f;
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Monnoroch, 2013-06-28
@Monnoroch

You probably have a typo:

public Block(int id, int meta) : this(id, meta, "Unknown") { }

Otherwise, yes, just the constructors will be called in an intuitive order. First directly called, then called first, and so on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question