From 21d19b74aafe812072f5859c972dcde8b2a53ed0 Mon Sep 17 00:00:00 2001 From: Mukul Palol Date: Mon, 18 Mar 2024 15:27:13 +0530 Subject: [PATCH 1/3] Remove getter setter using properties --- Problem Statement- GettersSetters.txt | 24 ++++++++++++++++++++++++ UsingProperties.cs | 8 ++++++++ 2 files changed, 32 insertions(+) create mode 100644 Problem Statement- GettersSetters.txt create mode 100644 UsingProperties.cs diff --git a/Problem Statement- GettersSetters.txt b/Problem Statement- GettersSetters.txt new file mode 100644 index 0000000..9d4b860 --- /dev/null +++ b/Problem Statement- GettersSetters.txt @@ -0,0 +1,24 @@ +// What are the ways you can remove Getters/Setters from below code. Find at least 3 ways for that. It's just an example you can write code in your favorative language. + +public class Person { + private String name; + private int age; + + // Getter methods + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + // Setter methods + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } +} diff --git a/UsingProperties.cs b/UsingProperties.cs new file mode 100644 index 0000000..4fd83d5 --- /dev/null +++ b/UsingProperties.cs @@ -0,0 +1,8 @@ +namespace RemoveGetterSetterWitProperties +{ + public class Person + { + public string Name { get; set; } + public int Age { get; set; } + } +} From 6dc156b60e60d34299a636c0a4bcc9015ebd47a7 Mon Sep 17 00:00:00 2001 From: Mukul Palol Date: Mon, 18 Mar 2024 15:36:15 +0530 Subject: [PATCH 2/3] Remove getter setter using constructor initializations --- UsingConstructorInitialization.cs | 14 ++++++++++++++ UsingProperties.cs | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 UsingConstructorInitialization.cs diff --git a/UsingConstructorInitialization.cs b/UsingConstructorInitialization.cs new file mode 100644 index 0000000..f6b2a80 --- /dev/null +++ b/UsingConstructorInitialization.cs @@ -0,0 +1,14 @@ +namespace RemoveGetterSetterWithConstructorInitialization +{ + public class Person + { + public string Name { get; } + public int Age { get; } + + public Person(string name, int age) + { + Name = name; + Age = age; + } + } +} diff --git a/UsingProperties.cs b/UsingProperties.cs index 4fd83d5..3eb6840 100644 --- a/UsingProperties.cs +++ b/UsingProperties.cs @@ -1,4 +1,4 @@ -namespace RemoveGetterSetterWitProperties +namespace RemoveGetterSetterWithProperties { public class Person { From 450e7c45d60e46b2d9d8d94d0f37cd3c0bd3e9d8 Mon Sep 17 00:00:00 2001 From: Mukul Palol Date: Mon, 18 Mar 2024 15:41:24 +0530 Subject: [PATCH 3/3] Remove getter setter using functions --- UsingFunctions.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 UsingFunctions.cs diff --git a/UsingFunctions.cs b/UsingFunctions.cs new file mode 100644 index 0000000..47da18e --- /dev/null +++ b/UsingFunctions.cs @@ -0,0 +1,17 @@ +namespace RemoveGetterSetterWithProperties +{ + public class Person + { + private string name; + private int age; + + public void SetPersonDetails(string name, int age) { + this.name = name; + this.age = age; + } + + public string GetPersonDetails() { + return $"Name: {name}, Age: {age}"; + } + } +}