Today's interview of C#
I think they are advanced topics.
C#
1. when will you use list, when will you use hashtable.
2. when will you use Idispose.
3.
Async and Await
public async Task<string> DoAsync5()
{
Task<string> mytask=Task.Run<string>(() =>
{
for (int j = 0; j < 20; j++)
{
System.Threading.Thread.Sleep(100);
Console.Write("------------Async thread: " + j.ToString() + "\r\n");
}
return "good";
});
return await mytask;
//return "good";
//Task.Factory.StartNew();
//IAsyncResult ar = mydoasync2.BeginInvoke(new AsyncCallback(CallbackMethod), mydoasync2);
//return result;
}
publicasyncTask<string> DoAsync()
{
Task<string> mytask = DoAsync5();
for (int i = 0; i < 100; i++)
{
System.Threading.Thread.Sleep(10);
Console.Write("Main thread: " + i.ToString() + "\r\n");
}
string mytaskResult = await mytask;
Console.Write("Both threads: " + mytaskResult);
return mytaskResult;
}
If you want to use await and async, you must call await twice.
1. In async function's definition.
2. In the excution funciton.
You must await a awaitable task!!!!!!
How to make a awaitable task. Define a task by task.run and await it......
It is strangely.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("主线程测试开始..");
AsyncMethod();
Thread.Sleep(1000);
Console.WriteLine("主线程测试结束..");
Console.ReadLine();
}
static async void AsyncMethod()
{
Console.WriteLine("开始异步代码");
var result = await MyMethod();
Console.WriteLine("异步代码执行完毕");
}
static async Task<int> MyMethod()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("异步执行" + i.ToString() + "..");
await Task.Delay(1000); //模拟耗时操作
}
return 0;
}
------------------------------------------------------------------------------------------
About begininvoker and callback.
you need to send the delegation itself to callback. If not, callback can't end this invoker correctly.
Now, there is theory and then there is practice. You have found, like
many other developers before you, that you can often get away with
ignoring this documented requirement. It may be an implementation detail
whether EndInvoke actually does anything that's absolutely
necessary to prevent your application from crashing, leaking memory,
etc. But here's the thing: if it's a documented requirement, you really ought to do it. This is not just about theory; it's about protecting yourself in the event of change.
By documenting this requirement, the designers of this asynchronous
calling mechanism basically gave themselves the freedom to change the
way BeginInvoke and EndInvoke work down the line so that, if there were sufficient reason (e.g., a performance enhancement), EndInvoke
could suddenly become a lot more necessary. Suppose it would suddenly
result in a deadlock if you forgot it. They've already covered
themselves by saying always call EndInvoke; if your app stops working because you didn't follow this requirement, the onus is on you.
I'm not saying this is necessarily a likely scenario. My point is simply that you shouldn't—or at least I wouldn't—ask "Is this really necessary?" with the mindset of If I can get away with leaving it out, then I will, since it is documented that you should do it.
http://stackoverflow.com/questions/4585042/is-delegate-endinvoke-really-necessary
Today's interview of C#的更多相关文章
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】
http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...
- WCF学习系列一【WCF Interview Questions-Part 1 翻译系列】
http://www.topwcftutorials.net/2012/08/wcf-faqs-part1.html WCF Interview Questions – Part 1 This WCF ...
- Amazon Interview | Set 27
Amazon Interview | Set 27 Hi, I was recently interviewed for SDE1 position for Amazon and got select ...
- Java Swing interview
http://www.careerride.com/Swing-AWT-Interview-Questions.aspx Swing interview questions and answers ...
- Pramp - mock interview experience
Pramp - mock interview experience February 23, 2016 Read the article today from hackerRank blog on ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- [译]Node.js Interview Questions and Answers (2017 Edition)
原文 Node.js Interview Questions for 2017 什么是error-first callback? 如何避免无止境的callback? 什么是Promises? 用什么工 ...
- WCF学习系列三--【WCF Interview Questions – Part 3 翻译系列】
http://www.topwcftutorials.net/2012/10/wcf-faqs-part3.html WCF Interview Questions – Part 3 This WCF ...
- WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】
WCF Interview Questions – Part 4 This WCF service tutorial is part-4 in series of WCF Interview Qu ...
随机推荐
- -_-#【Canvas】FPS
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- (2015年郑州轻工业学院ACM校赛题) A 彩票
这是个简单的题目,其实就是判断是否是偶数, 对二进行特判一下就行了! 比赛时候我们还错两次................ 一看简单题就想抢一血,谁知到第一次提交CE, 再提交WA 汗........ ...
- delphi操作文本文件的方法简介
delphi操作文本文件的方法简介减小字体 增大字体 作者佚名来源不详发布时间2008-5-31 10:31:16发布人xuedelphi1 文件类型和标准过程 Delphi同Object ...
- 辗转相除法_欧几里得算法_java的实现(求最大公约数)
辗转相除法,又被称为欧几里德(Euclidean)算法, 是求最大公约数的算法. 当然也可以求最小公倍数. 算法描述 两个数a,b的最大公约数记为GCD(a,b).a,b的最大公约数是两个数的公共素因 ...
- [LeetCode] 205. Isomorphic Strings 解题思路 - Java
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Unity绘制GUI连连看(尚未完善效果和重置)
OneImage.cs public class OneImage : MonoBehaviour { public int row, col; public Rect rect; public Te ...
- Objective-C中变量采用@property的各个属性值的含义
我们在OC中定义变量,可以自己来定义变量的setter方法来设置变量值,用getter方法来获取变量值.但是当变量数量增多时,还采用手动添加setter/getter方法来操作变量,就会使得程序代码量 ...
- 第二个参数(那个 properties)确定你将如何使用这个特性值
CBCharacteristicPropertyBroadcast: 允许一个广播特性值,用于描述特性配置,不允许本地特性 CBCharacteristicPropertyRead: 允许读一个特性值 ...
- Android 物理按键
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view. ...
- Java基础知识强化81:Math类random()方法之获取任意范围的随机数案例(面试题)
1. 需求:设计一个方法,可以实现获取任意范围内的随机数 分析:使用方法random()如下: public static double random() 注:Returns a pseudo-ran ...