-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass.cs
78 lines (62 loc) · 1.39 KB
/
class.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
class Person
{
// Field
private string _name;
// Properties
public string Name
{
set
{
_name = value;
}
get
{
return _name;
}
}
// Auto property
public DateTime Birthdate { get; set; }
// Constructors
// Default constructor used to initialize fields to thier default values
public Person()
{
}
public Person(string name)
{
this.Name = name;
}
// Constructor chaining
public Person(string name, DateTime birthdate)
: this(name)
{
this.Birthdate = birthdate;
}
// Method
public int GetAge()
{
// ⚡ This is not a correct way to calculate age from birthdate
return DateTime.Today.Year - Birthdate.Year;
}
}
class Program
{
static void Main(string[] args)
{
// Creating new instance of Person class
// using parameterless constructor
var p = new Person();
Console.WriteLine(p.Name); // null
Console.WriteLine(p.Birthdate); // 1/1/0001 12:00:00 AM
// using parameterized constructors
var p2 = new Person("Adam");
Console.WriteLine(p2.Name); // Adam
var p3 = new Person("Isaac", DateTime.Parse("02/20/2000"));
Console.WriteLine(p3.Name); // Isaac
Console.WriteLine(p3.GetAge());
var p4 = p3; // p4 & p3 points to the same object
p4.Name = "Mohamed";
Console.WriteLine(p3.Name); // Mohamed
Console.ReadKey();
}
}