例子
以下是我在Pete on Software博客上找到的代码示例:
var listThree = new string[] { "Pete", "On", "Software" };
var listFour = new string[] { "Joel", "On", "Software" };
stringExcept = listThree.Except(listFour);
Run Code Online (Sandbox Code Playgroud)
代码编译并运行。到目前为止,一切都很好。
问题
但是,我不明白为什么它有效。
那么,谁能解释为什么我可以Enumerable.Except在字符串数组上使用?
也许,如果有人可以解释如何阅读签名Enumerable.Except并给我一个代码示例,我会很清楚:
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> first,
IEnumerable<TSource> second
)
Run Code Online (Sandbox Code Playgroud)
我知道的
我知道泛型和扩展方法的概念。但显然还不足以理解上面的代码示例。我也已经使用过一些基本的 Linq 查询。
题
我想将键盘快捷键Ctrl+ ,(控制加逗号)分配给"首选项..."菜单项.我怎么做?
有一个Key.OemComma在Key枚举.我使用Key.OemComma如下面的代码示例所示.这在功能上非常好.但是GUI-wise:菜单项显示为
代替
示例代码
InputGestureCollection keyInputs = new InputGestureCollection();
keyInputs.Add(new KeyGesture(Key.OemComma, ModifierKeys.Control));
preferencesCommand = new RoutedUICommand("Preferences...", "Preferences", typeof(MyCommands), keyInputs);
Run Code Online (Sandbox Code Playgroud) 例
此测试失败,因为each不修改列表的元素.
void test_each_DoesNotModifyListElements() {
List<String> list = ["a", "b", "c"]
list.each { it = it + "z" }
assert ["az", "bz", "cz"] == list
}
Run Code Online (Sandbox Code Playgroud)
此测试通过,因为collect返回包含已修改元素的新列表.
void test_collect_ReturnsNewListWithModifiedElements() {
List<String> list = ["a", "b", "c"]
list = list.collect{ it + "z" }
assert ["az", "bz", "cz"] == list
}
Run Code Online (Sandbox Code Playgroud)
假设
在这一点上,我假设each不修改列表的元素,因为我已经编写了下面的测试,我假设这是测试告诉我的.如果我错了,请纠正我.
void test_each_DoesNotModifyListElements1() {
List<String> list = ["a", "b", "c"]
list.each { it + "z" }
assert ["az", "bz", "cz"] == list
} …Run Code Online (Sandbox Code Playgroud) Eclipse有一个很棒的东西叫做Quick Outline CTRL+ O.我想要类似但有类的东西,因为我正在进入一个包含大量类的新项目,并且不知道它们来自哪个源文件.
示例:按CTRL+ Shift+ O列出所有类,开始键入它修改列表,按Enter键,打开该源文件.
Eclipse中是否存在此功能?我查看Keys菜单,找不到任何东西,可能没有使用正确的名称.
题
没有static关键字的示例1 有效.
带static关键字的示例2 不起作用.
为什么不能在顶级类StaticClass是static?Eclipse说:
Groovy:类'StaticClass'具有不正确的修饰符static.
该类StaticInnerClass可以修改为static.
示例1有效
Start.groovy
assert "static field" == StaticClass.StaticInnerClass.staticField
Run Code Online (Sandbox Code Playgroud)
StaticClass.groovy
class StaticClass { // Just "class", no "static" modifier
static class StaticInnerClass {
static String staticField = "static field"
}
}
Run Code Online (Sandbox Code Playgroud)
示例2不起作用
Start.groovy
assert "static field" == StaticClass.StaticInnerClass.staticField
Run Code Online (Sandbox Code Playgroud)
StaticClass.groovy
static class StaticClass { // Modifier "static" does not work
static class StaticInnerClass {
static String staticField = "static field"
}
}
Run Code Online (Sandbox Code Playgroud)
可能是相关的问题 …
题
如何在没有继承方法的情况下获取类的所有方法的名称?
例
def methods = MyClass.methods.collect { it.name }
println methods.each { println it }
assert ["method1_static_void", "method2_static_String", "method3_void", "method4_String"].sort() == methods.sort()
class MyClass {
public static void method1_static_void() {}
public static String method2_static_String() {}
public void method3_void() {}
private String method4_String() {}
}
Run Code Online (Sandbox Code Playgroud)
预期产出
method1_static_void
method2_static_String
method3_void
method4_String
Run Code Online (Sandbox Code Playgroud)
实际输出
setProperty
getProperty
super$1$wait
super$1$wait
super$1$wait
super$1$clone
getMetaClass
invokeMethod
setMetaClass
__$swapInit
method3_void
method1_static_void
method2_static_String
this$2$method4_String
this$dist$invoke$1
this$dist$set$1
this$dist$get$1
super$1$toString
super$1$notify
super$1$notifyAll
super$1$getClass
super$1$equals
super$1$hashCode
super$1$finalize
wait
wait
wait
equals
toString …Run Code Online (Sandbox Code Playgroud) 例子
给定变量名称myVariableName和用 表示的光标位置[cursor]
String [cursor]myVariablenName = new String()
Run Code Online (Sandbox Code Playgroud)
什么键盘快捷键可以选择整个变量?
阐述
Ctrl+ Shift+Right仅选择my.
Ctrl+ Shift+ Right+ Right+Right选择整个变量名myVariableName,但击键太多。
还是扩展到git push origin <current-branch-name>?
git push [remote-name] [branch-name]