我有一组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
我似乎无法找到任何此类内容,并且想知道是否可以将函数或函数引用存储为数组元素的值.例如
array("someFunc" => &x(), "anotherFunc" => $this->anotherFunc())
Run Code Online (Sandbox Code Playgroud)
谢谢!
我写了一个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 …
开门见山:
int? i = null;
i.ToString(); //happy
i.GetType(); //not happy
Run Code Online (Sandbox Code Playgroud)
我得到一个非常相关的问题,它实际上解决了为什么i.ToString()工作正常.
编辑:刚刚发现这个角落的案例是这个SO线程中投票最多的案例!
我的第一个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#?
随着lambda表达式(内联代码)等新功能的出现,它是否意味着我们不再需要使用委托或匿名方法了?在我看过的几乎所有样本中,都是使用新语法进行重写.
我们仍然需要使用delegates和lambda表达式的任何地方都不起作用?
我会告诉我的要求.我需要keydown在Windows窗体表单中为每个控件创建一个事件.如果我必须为所有keydown事件做的事情是相同的,那么最好这样做而不是为所有控件手动执行它.
所以我基本上可以这样做:
foreach (Control c in this.Controls)
c.KeyDown+= new KeyEventHandler(c_KeyDown);
Run Code Online (Sandbox Code Playgroud)
但是在这里,foreach不会循环驻留在groupBox或tabControl中的那些控件.我的意思是如果表单(this)包含groupBox或其他一些容器控件,那么我可以获得该特定容器控件的keydown事件.并且foreach不会循环遍历该容器控件内的控件.
问题1:如何为表单中的"所有"控件获取keydown事件?
如果上面的谜题得到解决,那么我的问题就结束了.
这是我可以做的事情:
foreach (Control c in this.Controls)
{
c.KeyDown += new KeyEventHandler(c_KeyDown);
if (c is Container control)
FunctionWhichGeneratesKeyDownForAllItsChildControls(c)
}
Run Code Online (Sandbox Code Playgroud)
我知道FunctionWhichGeneratesKeyDownForAllItsChildControls(c)如果组合框中有组框,我将不得不经历多次以获得所有控件的keydown.我能做到.我的问题是,
问题2:如何检查是否c是容器控件?
可以说我有一个植物表:
id fruit
1 banana
2 apple
3 orange
Run Code Online (Sandbox Code Playgroud)
我可以做到这些
SELECT * FROM plant ORDER BY id;
SELECT * FROM plant ORDER BY fruit DESC;
Run Code Online (Sandbox Code Playgroud)
这是显而易见的事情.
但我被这个咬了,这有什么作用?
SELECT * FROM plant ORDER BY SUM(id);
SELECT * FROM plant ORDER BY COUNT(fruit);
SELECT * FROM plant ORDER BY COUNT(*);
SELECT * FROM plant ORDER BY SUM(1) DESC;
Run Code Online (Sandbox Code Playgroud)
所有这些只返回第一行(id = 1).
ORDER BY什么?这是一个后续问题: List <T> .Contains和T [].包含的行为不同
T[].ContainsT类和结构时表现不同.假设我有这个结构:
public struct Animal : IEquatable<Animal>
{
public string Name { get; set; }
public bool Equals(Animal other) //<- he is the man
{
return Name == other.Name;
}
public override bool Equals(object obj)
{
return Equals((Animal)obj);
}
public override int GetHashCode()
{
return Name == null ? 0 : Name.GetHashCode();
}
}
var animals = new[] { new Animal { Name = "Fred" } };
animals.Contains(new Animal { Name = …Run Code Online (Sandbox Code Playgroud) 我这样做了:
public class LambdaConflict
{
public static void main(String args[]){
//*
System.out.println(LambdaConflict.get(
(str) -> "Hello World!! By ME?"
));
/*/
System.out.println(LambdaConflict.get(new Intf<String> (){
@Override public String get1(String str){
return "Hello World!! By get1 " + str;
}
}));
/*****/
}
public static String get(Intf<String> i, boolean b){
return i.get1("from 1");
}
}
interface Intf<T>
{
public T get1(T arg1);
public T get2(T arg1);
}
Run Code Online (Sandbox Code Playgroud)
并获得此异常:
不兼容的类型:Intf不是函数接口在接口Intf中找到的多个非重写抽象方法注意:某些消息已被简化; 使用-Xdiags重新编译:详细以获得完整输出1错误
是否有任何条件我不能使用lambda来替换匿名类?