Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# method created with this() in header
Message
From
26/03/2013 09:56:57
 
 
To
26/03/2013 09:30:40
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01569233
Message ID:
01569250
Views:
35
>Thanks a lot, I hope I see it. So even if I create several constructors, ":this()" makes the parametherless constructor to be called.
>But when or why can it be needed?

Because you do not want to duplicate code - look what happens here
	public static class qqq
	{
		static void Main()
		{
			A a = new A(2, 3);

			Console.ReadLine();

		}
	}


	class A
	{
		int Prop1;
		int Prop2;
		int Prop3;

		public A()
		{
			Prop1 = 1;
			Console.WriteLine("prop1");
		}

		public A(int prop2)
			: this()
		{
			Prop2 = prop2;
			Console.WriteLine("prop2");
		}

		public A(int prop2, int prop3)
			: this(prop2)
		{
			Prop3 = prop3;
			Console.WriteLine("prop3");
		}
	}
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform