Link to home
Start Free TrialLog in
Avatar of silentthread2k
silentthread2kFlag for United States of America

asked on

Implicitly and Explicitly calling default constructor of a class in C# 3.0

Hi, I have 2 questions about C# 3.0.......

What is the difference between
"implicitly calling the default constructor"
and
"explicitly calling the default constructor"
of a class?

Any advantages or reason to use a particular one?
(see attached code snippet)

class Program
    {
        static void Main(string[] args)
        {
            // example 1, old school
            MyClass mclass1 = new MyClass();
            Console.WriteLine(mclass1.mystring);

            //example 2, new with C# 3.0, implicitly calls the classes default constructor.
            MyClass mclass2 = new MyClass { mystring = "test2" };
            Console.WriteLine(mclass2.mystring);

            //example 2, new with C# 3.0, explicitly calls the classes default constructor.
            MyClass mclass3 = new MyClass () { mystring = "test3" };
            Console.WriteLine(mclass3.mystring);

            Console.ReadLine();
        }
    }

    class MyClass 
    {
        public string mystring { get; set; }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of silentthread2k

ASKER

Thanks, I will award you points, but I need more clarification...

What do does "implicitly" and "explicitly" mean in the context it is being used.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial