New Member FAQ | Forums | Earn Revenue | Posting Guidelines | Help Topics | Admissions 2013
Awards & Gifts
 
Login Login    Register      

ArticlesPractice TestsAsk ExpertsQuestion PapersJobsUniversitiesCollegesCoursesSchoolsTraining

Active Members
TodayLast 7 Daysmore...

Join our online Google+ community for Bloggers, Content Writers and Webmasters




Simple Program Using Constructor in C#


Posted Date:     Total Responses: 0    Posted By: darshan patel   Member Level: Silver   Points/Cash: 8   


Here First I will show you how to Create a constructor.

Create a Constructor:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Constructor1
{
class Program
{
public Program() //create a constructor with the same name of the class.
{
Console.WriteLine("This is constructor");
}

static void Main(string[] args)
{
Program p = new Program();//create a object when the constructor is called.

}
}
}


Output

:This is constructor

How to Overload Constructor

Overload a Constructor



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace consructoroverloading
{
class man //class
{
private string name; //declare variable
private int age;

public man(string theName, int theAge)//create a first constructor
{
name = theName;
age = theAge;
Console.WriteLine("man:{0} {1}", theName, theAge);
}
public man(string theName)//create a second constructor
{
name = theName;
Console.WriteLine("man:{0}", theName);
}
public man(int theAge)//create a third constructor
{
age = theAge;
Console.WriteLine("man:{0}",theAge);
}
static void Main(string[] args)
{
//create a different object for three consructor and pass the value
man m1 = new man("m1", 22);
man m2 = new man("m2");
man m3 = new man(23);


}
}
}

Output:


man:m1 22
man:m2
man:23


Attachments

  • source code (5240-17612-Constructor.rar)

  • Project Feedbacks


    No feedbacks found. Be the first to respond...

    Post Feedback
    You must Sign In to post a feedback.
    Next Project: Animation of SHREE GANESH( text ) using C computer graphics
    Previous Project: Encyption Using Auto Cipher Algorithm

    Return to Project Index

    Post New Project


    Related Projects

    Subscribe to Email
  • Get Jobs by Email
  • Forum posts by Email
  • Articles by Email
  • Online MembersJebaprincy
    Anand Gaurav
    bhavani shankar
    usha
    ARJUN
    Vandana
    Adesola Adeyeye
    JYOTI UTEKAR
    Farid Akhtar
    Ananthalakshmi
    khushboo ranka
    More...


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    AdSense Revenue Sharing sites   Advertise   Talk to Tony John
    ISC Technologies, Kochi - India. Copyright © All Rights Reserved.