我有一个枚举类型,我想定义的>,<,>=,和<=运营商.我知道这些运算符是在枚举类型的基础上隐含创建的(根据文档)但我想明确定义这些运算符(为了清晰,控制,知道如何操作等等)
我希望我可以这样做:
public enum SizeType
{
Small = 0,
Medium = 1,
Large = 2,
ExtraLarge = 3
}
public SizeType operator >(SizeType x, SizeType y)
{
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用("意外的令牌")......这可能吗?它似乎应该是因为有隐含定义的运算符.有什么建议?
这是我缩短的抽象类:
abstract class Report {
protected internal abstract string[] Headers { get; protected set; }
}
Run Code Online (Sandbox Code Playgroud)
这是一个派生类:
class OnlineStatusReport : Report {
static string[] headers = new string[] {
"Time",
"Message"
}
protected internal override string[] Headers {
get { return headers; }
protected set { headers = value; }
}
internal OnlineStatusReport() {
Headers = headers;
}
}
Run Code Online (Sandbox Code Playgroud)
我的想法是,我希望能够Report.Headers从程序集中的任何地方调用,但只允许它由派生类设置.我尝试Headers只做内部,但受保护不算比内部更严格.有没有办法使Headers内部及其set访问器受到保护和内部?
我觉得我非常滥用访问修饰符,所以任何设计帮助都会非常感激.
也许这个问题有点困惑.我将以更好的方式解释这个代码
string myfirststring = "hello1,hello123,content";
string_2 = "ent";
if (myfirststring.Contains(string_2)){
Console.WriteLine("Yes! this is included in the 1 string");
//then check if string_2 is equal to the 3 element of 1 string: content
}
Run Code Online (Sandbox Code Playgroud)
现在我想检查我的包含字符串"string_2"是否完全等于myfirststring的3个元素:content(在这种情况下不是因为值是ent而不是内容所以ent!= content)所以我该如何检查?
我知道这有多个其他线程,但我无法理解为什么
public int[] practice_5(List<int> items)
{
if (items == null)
{
return null;
}
else
{
List<int> items_sorted = items.OrderBy(p => p).ToList();
return items_sorted;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我已经正确地对项目列表进行了排序.我假设,但无论我尝试什么解决方法,它都不会返回它,因为它无法将类型转换List<int>为int[]?
我必须items_sorted在返回之前转换变量吗?