可能重复:
如何更改默认(T)在C#中返回的内容
print(default(int) == 0) //true
Run Code Online (Sandbox Code Playgroud)
同样,如果我有一个自定义对象,它的默认值将为null.
print(default(Foo) == null) //true
Run Code Online (Sandbox Code Playgroud)
我可以为自定义值default(Foo)而不是null吗?
例如,像这样:
public static override Foo default()
{
return new Foo();
}
Run Code Online (Sandbox Code Playgroud)
这不会编译.谢谢..
这是此问题的后续内容:对通用枚举集合应用的Cast <int> .Cast <int?>会导致无效的强制转换异常
enum Gender { Male, Female }
Gender g = Gender.Male;
bool b = g is int; // false, alright no issues
b = new[] { g } is IEnumerable<int>; // false, alright no issues
b = Is<Gender, int>(g); //false, alright no issues
b = Is<Gender[], IEnumerable<int>>(new[] { g }); // true, why on earth !!!
static bool Is<S, T>(S s)
{
return s is T;
}
Run Code Online (Sandbox Code Playgroud)
为什么在一般情况下Gender[] is IEnumerable<int>返回true?特别是当它们不兼容时?
IEnumerable<int> c …Run Code Online (Sandbox Code Playgroud) 在C#中,这两行代码之间有什么区别(如果有的话)?
tmrMain.Elapsed += new ElapsedEventHandler(tmrMain_Tick);
Run Code Online (Sandbox Code Playgroud)
和
tmrMain.Elapsed += tmrMain_Tick;
Run Code Online (Sandbox Code Playgroud)
两者看起来完全相同.当你输入后者时,C#是否只是假设你是指前者?
我想改变默认(T)对某些类的行为.因此,我想返回一个null对象,而不是为我的引用类型返回null.
有一些像
kids.Clear();
var kid = kids.Where(k => k.Age < 10).SingleOrDefault();
if (kid is NullKid)
{
Console.Out.WriteLine("Jippeie");
}
Run Code Online (Sandbox Code Playgroud)
任何人都知道这是否可能?
如果我没有得到这个非常错误,这种行为对我来说很奇怪.而不是解释,我将在下面发布一个示例代码,请告诉我为什么我得到输出x而不是y.
private void button1_Click(object sender, EventArgs e)
{
List<int> l = new List<int>() { 1, 2, 3 };
Fuss(l);
MessageBox.Show(l.Count.ToString());
}
private void Fuss(List<int> l)
{
l.Add(4);
l.Add(5);
}
Run Code Online (Sandbox Code Playgroud)
输出应该,我假设是3.但我得到输出为5.我理解输出可以是5如果我这样做:
private void button1_Click(object sender, EventArgs e)
{
List<int> l = new List<int>() { 1, 2, 3 };
Fuss(ref l);
MessageBox.Show(l.Count.ToString());
}
private void Fuss(ref List<int> l)
{
l.Add(4);
l.Add(5);
}
Run Code Online (Sandbox Code Playgroud) 如何使用LINQ或Lambda Expression过滤两个字符串的查询间隔.
例:
SELECT * FROM dbo.Country WHERE Name BETWEEN "Argentina" AND "Jamaica";
Run Code Online (Sandbox Code Playgroud) 如果我们可以通过将类中的所有成员都设置为抽象来实现接口功能,那么为什么抽象类和接口都存在于C#中.
是因为:
请澄清
我有一组2D数组.例如,它就像:
{{{0, 0, 1}, {1, 0, 0}}
{{0, 0, 3}, {2, 1, 2}, {2, 2, 1}, {3, 0, 0}}
{{0, 0, 7}, {3, 2, 6}, {6, 2, 3}, {6, 3, 2}, {7, 0, 0}}}
Run Code Online (Sandbox Code Playgroud)
但如果我写
int [,][] arrays={{{0, 0, 1}, {1, 0, 0}}
{{0, 0, 3}, {2, 1, 2}, {2, 2, 1}, {3, 0, 0}}
{{0, 0, 7}, {3, 2, 6}, {6, 2, 3}, {6, 3, 2}, {7, 0, 0}}};
Run Code Online (Sandbox Code Playgroud)
编译器会抱怨";期望".
如果我写
int [,][] arrays={new int[,] {{0, 0, 1}, {1, …Run Code Online (Sandbox Code Playgroud) c# initialization multidimensional-array array-initialization
我写了一个SplitBetween类似的LINQ扩展方法String.Split.
> new List<int>(){3,4,2,21,3,2,17,16,1}
> .SplitBetween(x=>x>=10)
[3,4,2], [3,2], [], [1]
Run Code Online (Sandbox Code Playgroud)
资源:
// partition sequence into sequence of contiguous subsequences
// behaves like String.Split
public static IEnumerable<IEnumerable<T>> SplitBetween<T>(this IEnumerable<T> source,
Func<T, bool> separatorSelector,
bool includeSeparator = false)
{
var l = new List<T>();
foreach (var x in source)
{
if (separatorSelector(x))
{
if (includeSeparator)
{
l.Add(x);
}
yield return l;
l = new List<T>();
}
else
{
l.Add(x);
}
}
yield return l;
}
Run Code Online (Sandbox Code Playgroud)
在LINQ的精神下,我认为这种方法应该做懒惰的评估.但是,我的实现对外部IEnumerable进行了懒惰的评估,但没有超过内部的IEnumerable …
我的第一个F#日.如果我有这个:
let cat = Animal()
Run Code Online (Sandbox Code Playgroud)
现在我如何在以后检查cat is Animal?
在C#中
bool b = cat is Animal;
Run Code Online (Sandbox Code Playgroud)
在F#?