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 ...
随机推荐
- (转)C++ 编程规范
转载地址:http://www.cnblogs.com/len3d/archive/2008/02/01/1061902.html C/C++编码规范 今天人们越来越明白软件设计更多地是一种工程,而不 ...
- HelloSilverlight
一:输入姓名并选中一个日期,将在下面显示 二:XAML代码 <UserControl x:Class="HelloSilverlight.MainPage" xmlns=&q ...
- [LeetCode] 219. Contains Duplicate II 解题思路
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- C++ 通过Thunk在WNDPROC中访问this指针实现细节
本文代码使用了一些C++11特性,需要编译器支持.本文仅讨论x86_64平台的相关实现,x86平台理论上只需修改 thunk 相关机器码即可. THUNK的原理参见之前的一篇博文<C++ 通过T ...
- JavaScript 构造函数
关于JavaScript构造函数,如今出现了很多JavaScript的框架,例如jQuery.Ext等等这些,这些将JavaScript作为一种面向对象的语言进行编程,那么JavaScript到底是怎 ...
- Mac下phpstorm 浏览器出现 502 bad gateway 解决办法
问题: 在网上搜了好久,都没有合适的解决办法,于是决定分享下自己鼓捣好久解决了的办法,希望能帮到大家. 出现上述情况,一般在phpstorm里都会出现这个提示 我们只需要点击蓝色的进去,或者点phps ...
- UITouch附加
框架 /System/Library/Frameworks/SpriteKit.framework 可用性 可用于iOS 7.0或者更晚的版本 声明于 SKNode.h 概览 重要提示:这是一个初步的 ...
- Java中迭代列表中数据时几种循环写法的效率比较
Java中经常会用到迭代列表数据的情况,本文针对几种常用的写法进行效率比较.虽然网上已经有了类似的文章,但是对他们的结论并不认同. 常见的实现方法: 1.for循环: for(int i = 0; i ...
- android窗体动画:activity启动从底部向上滑动出现,关闭的时候从顶部向下滑动消失的动画实现
在IOS系统里,我们打开app都是有启动动画的,这样子在打开一个新窗体,用户感觉有个过渡动画,体验效果很好,不会觉得很突然就冒出一个窗体,下面就实现一个android 的activity启动和关闭动画 ...
- ceph主要数据结构解析3-Ceph_fs.h文件
(1)集群内部子版本协议类型宏定义:与公共协议保持独立性,以便消息类型和协议升级受影响 #define CEPH_OSDC_PROTOCOL 24 /* server/client */OSD服务 ...