Obtains the longest or shortest length of an element in a string array.

Source: Internet
Author: User

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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.