-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.cs
108 lines (92 loc) · 3.02 KB
/
classes.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
namespace ClassDemo
{
// This class demonstrates various ways to declare a class,
// different property and field implementations, and object creation methods.
public class Person
{
// Private fields
// usually their names start with _ (underscore)
private string _firstName;
private string _lastName;
private int _age;
// Auto-implemented property
public string Email { get; set; }
// Property with a backing field
private string _phoneNumber;
public string PhoneNumber
{
get { return _phoneNumber; }
set { _phoneNumber = value; }
}
// Read-only property
public string FullName
{
get { return $"{_firstName} {_lastName}"; }
}
// Property with private setter
public int Age
{
get { return _age; }
private set { _age = value; }
}
// Static members ( a static property)
public static int Population { get; private set; }
// Default constructor
public Person()
{
_firstName = "Unknown";
_lastName = "Unknown";
Age = 0;
Population++;
}
// Parameterized constructor
public Person(string firstName, string lastName, int age)
{
_firstName = firstName;
_lastName = lastName;
Age = age;
Population++;
}
// Method to demonstrate changing private field through public method
public void UpdateName(string firstName, string lastName)
{
_firstName = firstName;
_lastName = lastName;
}
// Method to print Person's detail
public string DisplayInfo()
{
return $"{FullName}, Age: {Age}, Email: {Email}, Phone: {PhoneNumber}";
}
// Static method
public static void ShowPopulation()
{
Console.WriteLine($"Current population: {Population} people created.");
}
}
class Program
{
static void Main()
{
// Object creation Using default constructor
Person person1 = new();
Console.WriteLine("Person 1:\n" + person1.DisplayInfo());
// Object creation Using parameterized constructor
Person person2 = new("Faqeer", "Hussain", 41);
person2.Email = "[email protected]";
person2.PhoneNumber = "03320684191";
Console.WriteLine("Person 2:\n" + person2.DisplayInfo());
// Object creation Using object initializer syntax
Person person3 = new()
{
Email = "[email protected]",
PhoneNumber = "0300000000000"
};
person3.UpdateName("Alice", "Brown");
Console.WriteLine("Person 3:\n" + person3.DisplayInfo());
// Accessing static method
Person.ShowPopulation();
}
}
}