Simple Program of Constructor in Console in ASP.NET using C#

Constructor
 
A Class Constructor is a special member function of a class that comes in existence when we create new object of that class.
Constructor is having the same name as that of class name. It doesn’t have any return type.
 
They are of three types:
  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
Step 1

Open your Visual Studio. By pressing Ctrl +Shift + N you will get your “New Project” Window.

 
Step2

After pressing OK you will get into your Coding Part where you will see three files in Solution Explorer [Properties, References, Program.cs], in which Program.cs file is your main file where you embed all your Inheritance program code.

 
This is the Code of your Constructor Program:
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;     
  5. namespace Constructor_demo3213213 {    
  6.     class sample {    
  7.         public sample() {    
  8.             Console.WriteLine("This is our Default Constructor");    
  9.             Console.WriteLine("-----------------------------------------");    
  10.         }    
  11.         public sample(string name) {    
  12.             Console.WriteLine("This is our Parametrized Constructor");    
  13.             Console.WriteLine("Name is :" + name);    
  14.         }    
  15.         public sample(string name, int age) {    
  16.             Console.WriteLine("Name is " + name);    
  17.             Console.WriteLine("Age is " + age);    
  18.         }    
  19.     }    
  20.     class Program {    
  21.         static void Main(string[] args) {    
  22.             sample s = new sample();    
  23.             sample s1 = new sample("Nilesh");    
  24.             sample s2 = new sample("24");    
  25.             Console.ReadKey();    
  26.     
  27.         }    
  28.     }    
  29. }  


Output
 
Press F5 to get your output, you will get something like this:



Hope you like it! Have a nice day Thank you for Reading!
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now