I want to override -- operator for MyString class. It should find the shortest word in string. Here is code:
class MyString
{
private string data;
public string Data
{
get { return data; }
set { data = value; }
}
public MyString(string s)
{
Data = s;
}
public MyString(MyString s)
{
Data = s.Data;
}
public static MyString operator --(MyString s)
{
var words = s.Data.Split();
int l = 99999, ind = 0;
for (int i = 0; i < words.Length; i++)
{
if (words[i].Length < l)
{
ind = i;
l = words[i].Length;
}
}
MyString result = new MyString(words[ind]);
return result;
}
}
When I try to use it like this:
MyString s1, shortest;
s1 = new MyString("one two three");
shortest = s1--;
Console.WriteLine(shortest.Data);
it returns "one two three" instead of "one". How can I fix it?