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

silentthread2kSenior Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richard LeeSoftware EnthusiastCommented:
Example 2 & 3 are the same. There is no difference between the statements. There is no advantage or change in how things work by explicitly or implicitly calling the default constructor.

DaTribe
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
silentthread2kSenior Software EngineerAuthor Commented:
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.
0
Miguel OzSoftware EngineerCommented:
This is the new syntax called "object initializer syntax"
Both approaches produce the same result, the difference is that "explicit constructor" clearly specifies you are using the defalut constructor
Check:
http://msdn.microsoft.com/en-us/library/bb397680.aspx
0
Miguel OzSoftware EngineerCommented:
Implict means you are using the "()" to specify you are using the default constructor. "Explicit" you are specifying the default constructor
0
Richard LeeSoftware EnthusiastCommented:
The option of specifying the constructor is provided with the new syntax in c# 3.0. If no constructor is specified then the default is chosen however you can also choose to use non-default constructors before the initialization of the properties.

var first = new MyClass { mystring = "test2" };
var second = new MyClass() { mystring = "test2" };

first & second are the same.

Now suppose there was a non-default constructor that accepts a string and sets mystring to that value.

var third = new MyClass("test3") { mystring = "test2" };

the value should be = test2

Of course no one would do this, but its useful in other cases. Dependency Injection, IoC, etc.

DaTribe
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.