Say, if I need to override ToString
method in a custom List
, I'd do this:
public class WebUILanguage2 : List<WebUILanguage>
{
public override string ToString()
{
return "Overridden message";
}
}
but what if I want to override this?
public class WebUILanguage2 : WebUILanguage[]
You can't. You can't derive from array types.
I'd generally advise against overriding ToString
in List<T>
, too - usually it's better to use composition than inheritance for things like this, in my experience.
As was said, you cant do that. But as a workaround, you can simply write a new method:
using System;
static class Program {
static string Str(this string[] a) => String.Join(',', a);
static void Main() {
string[] a = {"May", "June"};
Console.WriteLine(a.Str());
}
}
Or function:
using System;
class Program {
static string Str(string[] a) => String.Join(',', a);
static void Main() {
string[] a = {"May", "June"};
Console.WriteLine(Str(a));
}
}
Or a generic function:
using System.Collections.Generic;
using System;
class Program {
static string Str(IEnumerable<string> a) => String.Join(',', a);
static void Main() {
string[] a = {"May", "June"};
Console.WriteLine(Str(a));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.