This topic has been archived. It cannot be replied.
-
工作学习 / 学科技术讨论 / 朋友最近找.net工作遇到的几道面试题,比较新鲜,给找工作的IT同行
-binghongcha76(一只大猫);
2009-7-22
{1193}
(#5434710@0)
-
The aggregate operator usage is very novel. Normally aggregate operators are primarily used for numeric accumulative operation.
-deep_blue(BLUE);
2009-7-24
(#5440878@0)
-
哈,深蓝终于出现了,正好有个问题问你。那个PLINQ不是有3种访问方法吗 Pipelined, stop-and-go, Inverted Enumeration 我看资料说Inverted Enumeration速度最快,可是我的测试结果却是Pipelined速度最快,这个你有经验吗?到底是怎么回事?
-binghongcha76(一只大猫);
2009-7-24
(#5441131@0)
-
Sorry I don’t test them. Recently, I have been being fully overloaded.
-deep_blue(BLUE);
2009-7-24
(#5441328@0)
-
string.Join("-", a);
-xmlhttprequest(build5381);
2009-7-24
(#5441387@0)
-
Join是个好方法,但是只能用于string[],aggregate 更通用一些,可以用于所有IEnumerable<>的东西啦
-binghongcha76(一只大猫);
2009-8-4
(#5461910@0)
-
再举一个面试题,和.Net 2.0的 yield 有关
-binghongcha76(一只大猫);
2009-8-23
{1295}
(#5501793@0)
-
问个知其所以然的问题:为什么我们需要yield这个命令。
-interview(intervieweree);
2009-8-24
(#5502722@0)
-
用yield的好处可能主要是这样
-binghongcha76(一只大猫);
2009-8-24
{798}
(#5502744@0)
-
这个例子举得太烂了,用好一点的例子来说明一下yield的用法
-binghongcha76(一只大猫);
2009-8-24
{1423}
(#5502820@0)
-
对不起,我老提不同意见。这个应该用多线程很容易实现,而且多线程可以中断后续的处理,比如你这个例子中,处理到1000时程序逻辑上发现不需再往下处理就结束了,yield能做到吗?(我是真不知道,向你学习才提的问题。)
-tjhong(我检讨);
2009-8-24
{183}
(#5503557@0)
-
俺没看懂你的多线成是什么意思,但根据你的描述,用yield应该这么来写static void Main(string[] args)
{
int counter = 0;
foreach (var num in getNumbers())
{
if (counter++ > 1000) break;
Console.WriteLine("I am geting numbers: " + num.ToString());
}
}
static IEnumerable<string> getNumbers()
{
int i = 0;
while (true)
{
yield return i++.ToString();
}
}
-binghongcha76(一只大猫);
2009-8-24
{485}
(#5503622@0)
-
难怪你对AsParallel推崇倍加。我的意思是在static IEnumerable<string> getNumbers()
里面,不过看你的答案,我猜yield可以做到。
-tjhong(我检讨);
2009-8-24
(#5503729@0)
-
对了,我记得VB里没有 yield return 这个关键字,很遗憾了,VB程序员享受不到C#的这点好处了
-binghongcha76(一只大猫);
2009-8-24
(#5503686@0)
-
Yield is not a feature of the .Net runtime. It is just a C# language feature which gets compiled into simple IL code by the C# compiler.
-tjhong(我检讨);
2009-8-24
(#5503718@0)
-
从功能上用backgroundWorker class也能轻松做到,不过形式上就不是IEnumerable和yield了。backgroundWorker 更强大,它可以每次返回不同type的值。
-tjhong(我检讨);
2009-8-25
(#5505163@0)
-
而且,通过我对Linq中where, select等等的测试,无论我查询或者选择多大的数据量,Linq总是能在第一时间开始返回IEnumerable结果,所以俺猜想Linq中的Where, Select等等里面全都是用的yield
-binghongcha76(一只大猫);
2009-8-24
(#5502783@0)
-
Typically, yield return uses lazily generating a sequence of objects.It just generates the next item on demand.
It means that yield return itself doesn’t create actual iterator. Only when the sequence is traversed, it goes thro each item.
One benefit is as follows.
One sequence might be infinite. If it requires return an infinite sequence, it’s impossible. However, when you call its top items, e.g. first 1000 (or some other conditions), you can get final result without caring about the infinite.
-deep_blue(BLUE);
2009-8-24
{445}
(#5503440@0)
-
继续挖根究底:那在什么情况下,我们需要使用infinite sequence?天文应用?银行计算,还是其他的?
-interview(intervieweree);
2009-8-24
(#5503643@0)
-
It’s just extreme example and program concept.yield return just tells .NET framework don’t run it until its sequence is actually traversed.
LZ’s interview question is also good example. If you loop thro whole GetNames method, it throws exception. However, if you run it in following, it runs properly.
var q = GetNames().Where(n => n.EndWith(“ang”));
foreach (var e in q)
{
…
}
-deep_blue(BLUE);
2009-8-25
{356}
(#5504735@0)
-
Sometimes, greedy operators are also important.
-deep_blue(BLUE);
2009-8-27
{657}
(#5511277@0)
-
在for each里做remove应该会有问题的。
-tjhong(我检讨);
2009-8-27
(#5511759@0)
-
Of cource, if without ToList().
-deep_blue(BLUE);
2009-8-27
(#5511865@0)
-
唔,指针。
-tjhong(我检讨);
2009-8-27
(#5512258@0)
-
Unlike lazy operator, greedy operator generates a status result set that is not affected by changing original data.
-deep_blue(BLUE);
2009-8-28
(#5512779@0)
-
我们本着共同学习,我玩了一个上午才发现的,如果这个ListBox1.Items里有多个同样值的listItem,用remove会有问题,得用removeat.
-tjhong(我检讨);
2009-8-28
(#5513379@0)
-
If I do it, I’ll filter out dups when adding/binding data source into the listbox.
-deep_blue(BLUE);
2009-8-28
(#5513619@0)
-
再举一个小例子,用C#的 params 关键字简化编程params关键字是和 ref, out 属于一类的,但是用params有时可以让变成更轻松一些,看一下这个例子
string a = ".....ooooo;;;;;";
如果在a后面打一个.Trim(,那么系统会弹出Trim的重载方法,其中一个重载方法是
a.Trim(params char[] trimChars);
它的作用是把字符串两边指定不用的字符删掉,如果要删掉 .和; , 我以前这么写
a.Trim(new char[] {'.',';'})
而使用了params修饰方法参数后,我们可以直接使用一组对象作为参数
a.Trim('.',';');
很方便,我再也不用打入那一大堆讨厌的new char[] {},或者 new string[] {}了
-binghongcha76(一只大猫);
2009-8-27
{510}
(#5510617@0)
-
呵呵,这次可不是C#的专利,不知为何要加这个keyword,"a.Trim(char[] trimChars);"不是也把意思表达啦。VB.NET是a.Trim(trimChars As char())。
-tjhong(我检讨);
2009-8-27
(#5510789@0)
-
呵呵,再看了一下,明白你的意思。
-tjhong(我检讨);
2009-8-27
(#5511120@0)
-
C++里有个...
-firetrain(火车头);
2009-8-28
(#5513499@0)
-
LINQ has been dead for while,难道还有人用吗?
-mfcguy();
2009-9-6
(#5531725@0)