Obtains the longest or shortest length of an element in a string array.
There is a string array below:
string[] elements = {"adsf","etwert" ,"asdfasd","gs"};
The length must be the longest or shortest length of the element.
You can create an object in the program. This object has two attribute element values and element length:
Class Class6 {private string _ ElementValue; public string ElementValue {get {return _ ElementValue;} set {_ ElementValue = value ;}} public int ElementLength {get {return _ ElementValue. length ;}} public Class6 (string v) {_ ElementValue = v ;}}Source Code
Next, we can create another object:
Class Class7 {private List <Class6> Elements = new List <Class6> (); public void Add (Class6 c6) {Elements. add (c6);} public int MaxLenth () {int max = int. minValue; foreach (Class6 c6 in Elements) {if (c6.ElementLength> max) {max = c6.ElementLength ;}} return max ;}public int MinLenth () {int min = int. maxValue; foreach (Class6 c6 in Elements) {if (c6.ElementLength <min) {min = c6.ElementLength ;}} return min ;}}Source Code
In the above object, it has three public methods: Add (), MaxLength (), and MinLength ().
Now, in the console application, test the code we wrote above:
OK, we have achieved the expected results.
However, according to the program encapsulation, the highlighted code below should not appear in the client program. What to do should be encapsulated in the Class7 category. Therefore, Insus. NET wants to modify it.
After this modification, the front-end code can directly pass in the array string: