where T : struct
Run Code Online (Sandbox Code Playgroud)
我们C#开发人员都知道C#的基础知识.我的意思是声明,条件,循环,运算符等.
我们中的一些人甚至掌握了Generics,匿名类型,lambdas,LINQ等......
但是C#粉丝,瘾君子,专家几乎都不知道C#最隐藏的功能或技巧是什么?
yield由迈克尔·葡萄汁var由迈克尔·葡萄汁using()kokos的声明readonly由kokosas由迈克·斯通as/ is由埃德Swangrenas/ is(改进)由Rocketpantsdefault由deathofratsglobal::通过pzycomanusing()由块AlexCusevolatile作者:JakubŠturcextern alias作者:JakubŠturc在以下两个片段中,第一个是安全的还是你必须做第二个?
安全我的意思是每个线程保证从创建线程的相同循环迭代中调用Foo上的方法?
或者你必须将引用复制到一个新的变量"local"到循环的每次迭代?
var threads = new List<Thread>();
foreach (Foo f in ListOfFoo)
{
Thread thread = new Thread(() => f.DoSomething());
threads.Add(thread);
thread.Start();
}
Run Code Online (Sandbox Code Playgroud)
-
var threads = new List<Thread>();
foreach (Foo f in ListOfFoo)
{
Foo f2 = f;
Thread thread = new Thread(() => f2.DoSomething());
threads.Add(thread);
thread.Start();
}
Run Code Online (Sandbox Code Playgroud)
更新:正如Jon Skeet的回答所指出的,这与线程没有任何关系.
我想使用这个Task <>构造函数.我似乎无法得到正确的sntax可以有人纠正我的代码.
另外,我是否正确地认为,如果一个任务是以这种方式构建的,那么它是不是已经开始了?
我认为我需要的构造函数:
Task<TResult>(Func<Object, TResult>, Object)
Run Code Online (Sandbox Code Playgroud)
我的代码错误:
参数1:无法从'方法组'转换为'
System.Func<object,int>'
static void Main(string[] args)
{
var t = new Task<int>(GetIntAsync, "3"); //error is on this line
...
}
static async Task<int> GetIntAsync(string callerThreadId)
{
...
return someInt;
}
Run Code Online (Sandbox Code Playgroud) 根据这个答案,当代码使用lambda方法内部的局部变量时,编译器将生成可以具有名称的额外类c__DisplayClass1.例如以下(完全无用的)代码:
class Program
{
static void Main()
{
try {
implMain();
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
static void implMain()
{
for (int i = 0; i < 10; i++) {
invoke(() => {
Console.WriteLine(i);
throw new InvalidOperationException();
});
}
}
static void invoke(Action what)
{
what();
}
}
Run Code Online (Sandbox Code Playgroud)
输出以下调用堆栈:
System.InvalidOperationException
at ConsoleApplication1.Program.<>c__DisplayClass2.<implMain>b__0()
at ConsoleApplication1.Program.invoke(Action what)
at ConsoleApplication1.Program.implMain()
at ConsoleApplication1.Program.Main()
Run Code Online (Sandbox Code Playgroud)
请注意,c__DisplayClass2其中有一个由编译器生成的用于保存循环变量的类的名称.
根据这个答案 c__DisplayClass "手段"
c - >匿名方法闭包类("DisplayClass")
好的,但"DisplayClass"在这里意味着什么?
这会产生什么类"显示"?换句话说,为什么它不是"MagicClass"或"GeneratedClass"或任何其他名称?
最近我遇到了这样的问题:
What numbers will be printed considering the following code:
class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 3, 5, 7, 9 };
int threshold = 6;
var query = from value in numbers where value >= threshold select value;
threshold = 3;
var result = query.ToList();
result.ForEach(Console.WriteLine);
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
回答: 3, 5, 7, 9
这让我很惊讶.我认为该threshold值将在查询构造中放入堆栈,稍后在执行时,该数字将被拉回并在条件中使用..这种情况没有发生.
另一种情况(在执行之前numbers设置null):
static void Main(string[] args)
{
int[] numbers = { 1, 3, …Run Code Online (Sandbox Code Playgroud) 我仍然不太清楚闭包是什么,所以我发布了这两个例子,我想知道这些例子是否都是闭包?
例A:
List<DirectoryInfo> subFolders = new List<DirectoryInfo>();
Action<string> FilterSubFoldersStartA =
s => subFolders.
AddRange((new DirectoryInfo(s)).GetDirectories().
Where(d => d.Name.StartsWith("A")));
FilterSubFoldersStartA(@"c:\tempa");
FilterSubFoldersStartA(@"c:\tempb");
Run Code Online (Sandbox Code Playgroud)
例B:
List<DirectoryInfo> subFolders = new List<DirectoryInfo>();
string filter = "A";
Action<string> FilterSubFoldersStartGen =
s => subFolders.
AddRange((new DirectoryInfo(s)).GetDirectories().
Where(d => d.Name.StartsWith(filter)));
FilterSubFoldersStartGen(@"c:\tempa");
filter = "B";
FilterSubFoldersStartGen(@"c:\tempb");
Run Code Online (Sandbox Code Playgroud) 可能重复:
.NET中的"闭包"是什么?
我目前正在研究lambda表达式,并且封闭一词不断涌现.有人可以用真正简单的语言向我解释.
在C#中,是否可以动态创建新函数来定义变量?
我知道
string getResult() {
if (a)
return "a";
return "b";
}
String result = getResult();
Run Code Online (Sandbox Code Playgroud)
是可能的,但我正在寻找类似的东西
String result = new string getResult() {
if (a)
return "a";
return "b";
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?如果是这样,有人会证明吗?
编辑 这是可能的
编辑:最终 - 解决方案
这是我野蛮地黑客攻击的最终结果
Func<string> getResult = () =>
{
switch (SC.Status)
{
case ServiceControllerStatus.Running:
return "Running";
case ServiceControllerStatus.Stopped:
return "Stopped";
case ServiceControllerStatus.Paused:
return "Paused";
case ServiceControllerStatus.StopPending:
return "Stopping";
case ServiceControllerStatus.StartPending:
return "Starting";
default:
return "Status Changing";
}
};
TrayIcon.Text = "Service Status - " + …Run Code Online (Sandbox Code Playgroud) c# ×9
closures ×5
lambda ×2
.net ×1
async-await ×1
enumeration ×1
function ×1
linq ×1
monads ×1
variables ×1