Gets the longest or shortest length of an element in a string array
Keywords:
C#
Attribute
There is a string array below:
string[] elements = {"adsf","etwert" ,"asdfasd","gs"};
The requirement is to get the longest or shortest length of the element.
You can create an object in your program that has two attribute element values and element lengths:
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
Among the objects above, it has three methods of public, Add(), MaxLength(), and MinLength().
Now, in the console application, we'll test the code we wrote above:
Ok, that's what we expected.
However, depending on the encapsulation of the program, the code highlighted below should not appear in the client's program.How to handle this should be encapsulated in Class7.Therefore,Insus.NET Want to change it.
With this change, the front-end code simply passes in the array string: