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#

时间: 2025-01-16 04:11:34

Today's interview of C#的相关文章

《Cracking the Coding Interview》——第16章:线程与锁——题目5

2014-04-27 20:16 题目:假设一个类Foo有三个公有的成员方法first().second().third().请用锁的方法来控制调用行为,使得他们的执行循序总是遵从first.second.third的顺序. 解法:你应该想到了用lock的方法类阻塞,不过这里面有个概念问题使得直接用ReentrantLock不能通过编译(对于一个锁对象,不同在A线程中锁定,又在B线程中解锁,不允许这样的归属关系),可以用Semaphore来达到相同的目的.请看下面的代码. 代码: 1 // 16

《Cracking the Coding Interview》——第16章:线程与锁——题目3

2014-04-27 19:26 题目:哲学家吃饭问题,死锁问题经典模型(专门用来黑哲学家的?). 解法:死锁四条件:1. 资源互斥.2. 请求保持.3. 非抢占.4. 循环等待.所以,某砖家拿起一只筷子后如果发现没有另一只了,就必须把手里这只筷子放下,这应该是通过破坏"请求保持"原则来防止死锁产生,请求资源失败时,连自己的资源也进一步释放,然后在下一轮里继续请求,直到成功执行. 代码: 1 // This is the class for chopsticks. 2 import j

《Cracking the Coding Interview》——第16章:线程与锁——题目2

2014-04-27 19:14 题目:如何测量上下文切换的时间? 解法:首先,上下文切换是什么,一搜就知道.对于这么一个极短的时间,要测量的话,可以通过放大N倍的方法.比如:有A和B两件事,并且经常一起发生,每件只需要花几纳秒.如果你把A事件连续做几百万次,而B时间只做了几次,这样就能排除B事件对于测量的影响.如果总时间S = mA + nB.当m >> n 时,A≈S / m.下面的测量方法类似于打乒乓球,在主线程和副线程间互相传递一个令牌,这个令牌可以是变量.管道之类的用于通信的工具.与

《Cracking the Coding Interview》——第16章:线程与锁——题目1

2014-04-27 19:09 题目:线程和进程有什么区别? 解法:理论题,操作系统教材上应该有很详细的解释.我回忆了一下,写了如下几点. 代码: 1 // 16.1 What is the difference between process and thread? 2 Answer: 3 Process: 4 1. Basic element of resource allocation in the operating system. 5 2. Possesses independent

Popular HashMap and ConcurrentHashMap Interview Questions

http://howtodoinjava.com/core-java/collections/popular-hashmap-and-concurrenthashmap-interview-questions/ Popular HashMap and ConcurrentHashMap Interview Questions June 14, 2013 by Lokesh Gupta In my previous post related to “How HashMap works in jav

Java Interview Reference Guide--reference

Part 1 http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/ Posted on January 24, 2014 by Nitin Kumar JAVA Object Oriented Concepts Java in based on Object Oriented concepts, which permits higher level of abstraction to solve any p

《Cracking the Coding Interview》——第18章:难题——题目10

2014-04-29 04:22 题目:给定一堆长度都相等的单词,和起点.终点两个单词,请从这堆单词中寻找一条变换路径,把起点词变成终点词,要求每次变换只能改一个字母. 解法:Leetcode中有Word Ladder,这题基本思路一致. 代码: 1 // 18.10 Given a list of words, all of same length. Given a source and a destionation words, you have to check if there exis

《Cracking the Coding Interview》——第18章:难题——题目9

2014-04-29 04:18 题目:有一连串的数被读入,设计一个数据结构,能随时返回当前所有数的中位数. 解法:用一个大顶堆,一个小顶堆将数分成数量最接近的两份,就能轻松得到中位数了. 代码: 1 // 18.9 A stream of integers are passed to you, you have to tell me the median as they keep coming in. 2 #include <climits> 3 #include <iostream&

《Cracking the Coding Interview》——第18章:难题——题目13

2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3)级别的时间和空间进行动态规划.这道题目和第17章的最后一题很像,由于这题的时间复杂度实在是高,我动手写了字典树进行加速.如果单纯用哈希表来作为词典,查询效率实际会达到O(n)级别,导致最终的算法复杂度为O(n^4).用字典树则可以加速到O(n^3),因为对于一个字符串"abcd",只需要