From 1eeb28d68e69f4e7ae8b394d2fffc371f778cd3b Mon Sep 17 00:00:00 2001 From: Poyam Sharma Date: Wed, 20 Mar 2024 09:36:38 +0530 Subject: [PATCH 1/2] Added solutions for Getter Setter Problem Statement --- Solution/Solution1.cs | 23 +++++++++++++++++++++++ Solution/Solution2.cs | 18 ++++++++++++++++++ Solution/Solution3.cs | 20 ++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 Solution/Solution1.cs create mode 100644 Solution/Solution2.cs create mode 100644 Solution/Solution3.cs diff --git a/Solution/Solution1.cs b/Solution/Solution1.cs new file mode 100644 index 0000000..0b41908 --- /dev/null +++ b/Solution/Solution1.cs @@ -0,0 +1,23 @@ +//Using actual methods instead of Getter Setters +using System; +public class Person { + private string name; + private int age; + + public void IntializePersonDetails(string name, int age) { + this.name = name; + this.age = age; + } + + public void DisplayPersonDetails() { + Console.WriteLine($"Name of the Person: {name}\nAge of the Person: {age}"); + } +} + +public class Program { + public static void Main() { + Person person = new Person(); + person.IntializePersonDetails("Poyam", 22); + person.DisplayPersonDetails(); + } +} \ No newline at end of file diff --git a/Solution/Solution2.cs b/Solution/Solution2.cs new file mode 100644 index 0000000..3ef6ec8 --- /dev/null +++ b/Solution/Solution2.cs @@ -0,0 +1,18 @@ +//Using Constructor Initialization +public class Person { + private String name; + private int age; + + public Person(String name, int age) { + this.name = name; + this.age = age; + } + + public String FetchName() { + return name; + } + + public int FetchAge() { + return age; + } +} \ No newline at end of file diff --git a/Solution/Solution3.cs b/Solution/Solution3.cs new file mode 100644 index 0000000..27111c8 --- /dev/null +++ b/Solution/Solution3.cs @@ -0,0 +1,20 @@ +//Using Read-only Properties +public class Person { + public string Name { get; } + public int Age { get; } + + public Person(string name, int age) { + Name = name; + Age = age; + } +} + +class Program { + static void Main(string[] args) { + Person person = new Person("Poyam", 22); + + // Accessing properties + Console.WriteLine("Name of the Person: " + person.Name); + Console.WriteLine("Age of the Person: " + person.Age); + } +} \ No newline at end of file From f88543387f51452ec08d4f7e24d766b767423b5b Mon Sep 17 00:00:00 2001 From: Poyam Sharma Date: Mon, 1 Apr 2024 10:41:36 +0530 Subject: [PATCH 2/2] Updated file names --- Solution/Solution2.cs | 18 ------------ .../{Solution1.cs => UsingActualMethods.cs} | 2 +- Solution/UsingConstructors.cs | 28 +++++++++++++++++++ ...olution3.cs => UsingReadOnlyProperties.cs} | 2 +- 4 files changed, 30 insertions(+), 20 deletions(-) delete mode 100644 Solution/Solution2.cs rename Solution/{Solution1.cs => UsingActualMethods.cs} (94%) create mode 100644 Solution/UsingConstructors.cs rename Solution/{Solution3.cs => UsingReadOnlyProperties.cs} (95%) diff --git a/Solution/Solution2.cs b/Solution/Solution2.cs deleted file mode 100644 index 3ef6ec8..0000000 --- a/Solution/Solution2.cs +++ /dev/null @@ -1,18 +0,0 @@ -//Using Constructor Initialization -public class Person { - private String name; - private int age; - - public Person(String name, int age) { - this.name = name; - this.age = age; - } - - public String FetchName() { - return name; - } - - public int FetchAge() { - return age; - } -} \ No newline at end of file diff --git a/Solution/Solution1.cs b/Solution/UsingActualMethods.cs similarity index 94% rename from Solution/Solution1.cs rename to Solution/UsingActualMethods.cs index 0b41908..4a075b0 100644 --- a/Solution/Solution1.cs +++ b/Solution/UsingActualMethods.cs @@ -14,7 +14,7 @@ public void DisplayPersonDetails() { } } -public class Program { +public class ProfileManager { public static void Main() { Person person = new Person(); person.IntializePersonDetails("Poyam", 22); diff --git a/Solution/UsingConstructors.cs b/Solution/UsingConstructors.cs new file mode 100644 index 0000000..b089a8a --- /dev/null +++ b/Solution/UsingConstructors.cs @@ -0,0 +1,28 @@ +//Using Constructor Initialization +using System; + +public class Person { + private string name; + private int age; + + public Person(string name, int age) { + this.name = name; + this.age = age; + } + + public string FetchName() { + return name; + } + + public int FetchAge() { + return age; + } +} + +public class ProfileManager { + public static void Main() { + Person person = new Person("Poyam", 22); + Console.WriteLine("Name is : " + person.FetchName()); + Console.WriteLine("Name is : " + person.FetchAge()); + } +} \ No newline at end of file diff --git a/Solution/Solution3.cs b/Solution/UsingReadOnlyProperties.cs similarity index 95% rename from Solution/Solution3.cs rename to Solution/UsingReadOnlyProperties.cs index 27111c8..515384d 100644 --- a/Solution/Solution3.cs +++ b/Solution/UsingReadOnlyProperties.cs @@ -9,7 +9,7 @@ public Person(string name, int age) { } } -class Program { +class ProfileManager { static void Main(string[] args) { Person person = new Person("Poyam", 22);