ASPForums.Net RSS Feed additions to the content that appears on ASPForums.Net(c) 2019 All rights reserved.What is parameterized constructor and advantages of parameterized constructor in C# and VB.Net<p>Hi <a class="username" rel="divyasha"> divyasha</a>,</p> <ul style="list-style-type: undefined;"> <li>A constructor with at least one parameter is called a parameterized constructor.</li> <li>A Class or Struct can have multiple parameterized constructors as long as they have different method signature. They follow the same concept of method overloading.</li> <li>Compiler provides Default Constructors only if there is no constructor (Default or Parameterized) defined in a class.</li> <li>Parameterized Constructors can exist even without the existence of Default Constructors.</li> <li>The advantage of a parameterized constructor is that you can initialize each instance of the class to different values.</li> </ul> <p><strong>Example:</strong></p> <p><span style="text-decoration: underline;">C#</span></p> <pre class="brush: csharp">using System; namespace Constructor { class parameterizedconstrctor { public int a, b; // declaring Paremetrized Constructor with ing x,y parameter public parameterizedconstrctor(int x, int y) { a = x; b = y; } } class MainClass { static void Main() { // Creating object of Parameterized Constructor and ing values parameterizedconstrctor pc = new parameterizedconstrctor(100, 175); Console.WriteLine(&#34;-----------parameterized constructor example---------------&#34;); Console.WriteLine(&#34;\t&#34;); Console.WriteLine(&#34;value of a = &#34; + pc.a ); Console.WriteLine(&#34;value of b = &#34; + pc.b); Console.Read(); } } }</pre> <p><span style="text-decoration: underline;">VB.Net</span></p> <pre class="brush: vb">Namespace Constructor Class parameterizedconstrctor Public a As Integer, b As Integer &#39; declaring Paremetrized Constructor with ing x,y parameter Public Sub New(x As Integer, y As Integer) a = x b = y End Sub End Class Class MainClass Private Shared Sub Main() &#39; Creating object of Parameterized Constructor and ing values Dim pc As New parameterizedconstrctor(100, 175) Console.WriteLine(&#34;-----------parameterized constructor example---------------&#34;) Console.WriteLine(vbTab) Console.WriteLine(&#34;value of a = &#34; + pc.a) Console.WriteLine(&#34;value of b = &#34; + pc.b) Console.Read() End Sub End Class End Namespace</pre> <p>For more details refer the below link.</p> <p><a href=""></a></p>, 03 Mar 2017 03:53:47 GMT