Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Basic Example of using Constructor
WhatsApp
Ujjwal Patel
Jan 19
2015
1.1
k
0
0
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
TestingOops
{
class
Employee
{
public
int
Empage
{
get
;
set
;
}
public
string
Empname
{
get
;
set
;
}
public
string
Empphonenumber
{
get
;
set
;
}
public
double
Empsalary
{
get
;
set
;
}
public
void
Bonus(
double
yearsbouns)
{
Empsalary = Empsalary + Empsalary * yearsbouns;
}
public
Employee(
int
empage,
string
empname,
string
empphonenumber,
double
empsalary)
// Defining a constructor and passing parameters
{
Empsalary = empsalary;
Empage = empage;
Empname = empname;
Empphonenumber = empphonenumber;
}
public
Employee()
//Defining this constructor as default.
{
}
}
class
Program
{
static
void
Main(
string
[] args)
{
Employee emp =
new
Employee
{
Empage=32,
Empname=
"Ujjwal"
,
Empphonenumber=
"9031-214-542"
,
Empsalary=50000
};
Console.WriteLine(
"Employee Name is : \'{0}\' and Age is {1}. {0} gets salary of $ {2} and his phone number is {3}"
,emp.Empname,emp.Empage,emp.Empsalary,emp.Empphonenumber);
emp.Bonus(0.05);
Console.WriteLine(
"{0}'s salary after bonus is $ {1}"
, emp.Empname, emp.Empsalary);
Employee Steve =
new
Employee(22,
"Steven Mathews"
,
"242-34-53"
,30000);
Console.WriteLine(
"Employee Name is : \'{0}\' and Age is {1}. {0} gets salary of $ {2} and his phone number is {3}"
, Steve.Empname, Steve.Empage, Steve.Empsalary, Steve.Empphonenumber);
Console.ReadKey();
}
}
}
C#
Constructor
Example Constructor
Up Next
Basic Example of using Constructor