c# - Reduce code duplicates -
Currently I have a lot of squares (5) who have only 2 properties but different names for different purposes Are:
public class class 1 {public class 1 () {} public string id {get; Set; } Public string value {get; Set; }} Public class class 2 {public class 2 () {} public string id {get; Set; } Public string value {get; Set; }} ........ public class class 5 {public class 5 () {} public string id {get; Set; } Public string value {get; Set; }} Then I have a method for each class that is going to list & lt; Class & gt; . Public Stable List & lt; Class 1 & gt; GetClass1 () {Dictionary & lt; String, string & gt; S = GetSomeResults1 (); & Lt; Class1 & gt; _s = new list & lt; Class 1 & gt; (); Foreign (in different items) {Class 1C = New Class 1 (); C. IID = Item. KA; C.value = item.Value; _s.Add (c); } Return _s; } Public static list & lt; Class2 & gt; GetClass2 () {dictionary & lt; String, string & gt; S = GetSomeResults2 (); & Lt; Class2 & gt; _s = new list & lt; Class 2 & gt; (); Fourhatch (in different items) {Class 2C = new class 2 (); C. IID = Item. KA; C.value = item.Value; _s.Add (c); } Return _s; } ...... public static list & lt; Class 5 & gt; GetClass5 () {dictionary & lt; String, string & gt; S = GetSomeResults5 (); & Lt; Class5 & gt; _s = new list & lt; Class 5 & gt; (); Foreign (Word Item S) {Class 5C = New Class 5 (); C. IID = Item. KA; C.value = item.Value; _s.Add (c); } Return _s; } Any advice how can I improve this code?
Use the base class to share the shared properties and actions:
public class BaseClass {public string id {get; Set; } Public string value {get; Set; } // Shared properties and methods} Public class Class1: BaseClass {// your properties and methods} Public class Class2: BaseClass {// your properties and methods}
Comments
Post a Comment