|
| 1 | + |
| 2 | +using System; |
| 3 | + |
| 4 | +namespace ClassDemo |
| 5 | +{ |
| 6 | + // This class demonstrates various ways to declare a class, |
| 7 | + // different property and field implementations, and object creation methods. |
| 8 | + public class Person |
| 9 | + { |
| 10 | + // Private fields |
| 11 | + // usually their names start with _ (underscore) |
| 12 | + private string _firstName; |
| 13 | + private string _lastName; |
| 14 | + private int _age; |
| 15 | + |
| 16 | + // Auto-implemented property |
| 17 | + public string Email { get; set; } |
| 18 | + |
| 19 | + // Property with a backing field |
| 20 | + private string _phoneNumber; |
| 21 | + public string PhoneNumber |
| 22 | + { |
| 23 | + get { return _phoneNumber; } |
| 24 | + set { _phoneNumber = value; } |
| 25 | + } |
| 26 | + |
| 27 | + // Read-only property |
| 28 | + public string FullName |
| 29 | + { |
| 30 | + get { return $"{_firstName} {_lastName}"; } |
| 31 | + } |
| 32 | + |
| 33 | + // Property with private setter |
| 34 | + public int Age |
| 35 | + { |
| 36 | + get { return _age; } |
| 37 | + private set { _age = value; } |
| 38 | + } |
| 39 | + |
| 40 | + // Static members ( a static property) |
| 41 | + public static int Population { get; private set; } |
| 42 | + |
| 43 | + // Default constructor |
| 44 | + public Person() |
| 45 | + { |
| 46 | + _firstName = "Unknown"; |
| 47 | + _lastName = "Unknown"; |
| 48 | + Age = 0; |
| 49 | + Population++; |
| 50 | + } |
| 51 | + |
| 52 | + // Parameterized constructor |
| 53 | + public Person(string firstName, string lastName, int age) |
| 54 | + { |
| 55 | + _firstName = firstName; |
| 56 | + _lastName = lastName; |
| 57 | + Age = age; |
| 58 | + Population++; |
| 59 | + } |
| 60 | + |
| 61 | + // Method to demonstrate changing private field through public method |
| 62 | + public void UpdateName(string firstName, string lastName) |
| 63 | + { |
| 64 | + _firstName = firstName; |
| 65 | + _lastName = lastName; |
| 66 | + } |
| 67 | + |
| 68 | + // Method to print Person's detail |
| 69 | + public string DisplayInfo() |
| 70 | + { |
| 71 | + return $"{FullName}, Age: {Age}, Email: {Email}, Phone: {PhoneNumber}"; |
| 72 | + } |
| 73 | + |
| 74 | + // Static method |
| 75 | + public static void ShowPopulation() |
| 76 | + { |
| 77 | + Console.WriteLine($"Current population: {Population} people created."); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + class Program |
| 82 | + { |
| 83 | + static void Main() |
| 84 | + { |
| 85 | + // Object creation Using default constructor |
| 86 | + Person person1 = new(); |
| 87 | + Console.WriteLine("Person 1:\n" + person1.DisplayInfo()); |
| 88 | + |
| 89 | + // Object creation Using parameterized constructor |
| 90 | + Person person2 = new("Faqeer", "Hussain", 41); |
| 91 | + person2.Email = "[email protected]"; |
| 92 | + person2.PhoneNumber = "03320684191"; |
| 93 | + Console.WriteLine("Person 2:\n" + person2.DisplayInfo()); |
| 94 | + |
| 95 | + // Object creation Using object initializer syntax |
| 96 | + Person person3 = new() |
| 97 | + { |
| 98 | + |
| 99 | + PhoneNumber = "0300000000000" |
| 100 | + }; |
| 101 | + person3.UpdateName("Alice", "Brown"); |
| 102 | + Console.WriteLine("Person 3:\n" + person3.DisplayInfo()); |
| 103 | + |
| 104 | + // Accessing static method |
| 105 | + Person.ShowPopulation(); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments