给定一个DateTime代表一个人的生日,我该如何计算他们的年龄? An easy to understand and simple solution. // Save today's date. var today = DateTime.Today; // Calculate the age. var age = today.Year - birthdate.Year; // Go back to the year the person was born in case of a leap year if (birthdate > today.AddYears(-age)) age--; However, this assumes you are looking for the western idea of age and
给定一个DateTime代表一个人的生日,我该如何计算他们的年龄? 一个容易理解和简单的解决方案。 // Save today's date. var today = DateTime.Today; // Calculate the age. var age = today.Year - birthdate.Year; // Go back to the year the person was born in case of a leap year if (birthdate > today.AddYears(-age)) age--; 然而,这假设你正在寻找西方的年龄思想,而不是使用东亚推算。 这是一种很奇怪的方法
I want to know everything about the yield statement, in an easy to understand form. I have read about the yield statement and its ease when implementing the iterator pattern. However, most of it is very dry. I would like to get under the covers and see how Microsoft handles return yield. Also, when do you use yield break? yield works by building a state machine internally. It stores the
我想以易懂的形式了解关于yield声明的所有内容。 在实现迭代器模式时,我已经阅读了yield语句和它的简易性。 但是,大部分都很干燥。 我想了解一下,看看微软如何处理回报率。 另外,你什么时候使用yield break? yield通过在内部构建状态机来工作。 它存储例程退出时的当前状态,并在下一次从该状态恢复。 您可以使用Reflector来了解编译器如何实现它。 当您想要停止返回结果时使用yield break 。 如果你没有yiel
When planning out my programs, I often start with a chain of thought like so: A football team is just a list of football players. Therefore, I should represent it with: var football_team = new List<FootballPlayer>(); The ordering of this list represent the order in which the players are listed in the roster. But I realize later that teams also have other properties, besides the mere
在规划我的节目时,我经常从一系列的想法开始,比如: 足球队只是一个足球运动员的名单。 因此,我应该用: var football_team = new List<FootballPlayer>(); 此列表的排序表示球员列入名单的顺序。 但我后来认识到,除了仅仅是球员名单之外,球队还有其他属性,必须记录下来。 例如,本赛季得分总数,当前预算,统一颜色,代表球队名称的string等。 那么我想: 好吧,足球队就像一个球员列表,但另外它有一
Ran across this line of code: FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); What do the two question marks mean, is it some kind of ternary operator? It's hard to look up in Google. It's the null coalescing operator, and quite like the ternary (immediate-if) operator. See also ?? Operator - MSDN. FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); expands to:
穿过这行代码: FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); 这两个问号是什么意思,它是一种三元运算符? 很难在Google中查找。 它是空合并运算符,与三元运算符(immediate-if)非常相似。 也可以看看 ?? 运营商 - MSDN。 FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); 扩展到: FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper(); 进一步扩展到
After discussion with colleagues regarding the use of the 'var' keyword in C# 3 I wondered what people's opinions were on the appropriate uses of type inference via var? For example I rather lazily used var in questionable circumstances, eg:- foreach(var item in someList) { // ... } // Type of 'item' not clear. var something = someObject.SomeProperty; // Type of 'something' not cle
在与同事讨论在C#3中使用'var'关键字之后,我想知道人们对通过var?进行类型推断的适当用法有何看法? 例如,我比较懒惰地在可疑情况下使用var,例如: - foreach(var item in someList) { // ... } // Type of 'item' not clear. var something = someObject.SomeProperty; // Type of 'something' not clear. var something = someMethod(); // Type of 'something' not clear. var更合理的用法如下: var l = n
I have a class property exposing an internal IList<> through System.Collections.ObjectModel.ReadOnlyCollection<> How can I pass a part of this ReadOnlyCollection<> without copying elements into a new array (I need a live view, and the target device is short on memory)? I'm targetting Compact Framework 2.0. 尝试使用yield返回枚举的方法: IEnumerable<T> FilterCollecti
我有一个类属性暴露内部IList <>通过 System.Collections.ObjectModel.ReadOnlyCollection<> 如何在不将元素复制到新数组中的情况下传递ReadOnlyCollection<>的一部分(我需要一个实时视图,并且目标设备在内存上很短)? 我的目标是Compact Framework 2.0。 尝试使用yield返回枚举的方法: IEnumerable<T> FilterCollection<T>( ReadOnlyCollection<T> input ) { foreach ( T item
This question already has an answer here: Why can't a duplicate variable name be declared in a nested local scope? 9 answers Why isn't the first gridView variable only accessible in the foreach loop scope ? It is, but the compiler (language definition) simply forbids overlapping scopes. A simplified version that yields the same error: { // outer block, scope for the second `i`
这个问题在这里已经有了答案: 为什么不能在嵌套的本地作用域中声明重复的变量名? 9个答案 为什么不是第一个gridView变量只能在foreach循环范围内访问? 它是,但编译器(语言定义)只是禁止重叠范围。 产生相同错误的简化版本: { // outer block, scope for the second `i` for(int i = 0; i < 10; i++) // nested 'i', the first we see { // scope of first `i` } int i = 3; /
Is there some rare language construct I haven't encountered (like the few I've learned recently, some on Stack Overflow) in C# to get a value representing the current iteration of a foreach loop? For instance, I currently do something like this depending on the circumstances: int i=0; foreach (Object o in collection) { // ... i++; } The foreach is for iterating over collection
是否有一些罕见的语言构造我没有遇到(像我最近学到的一些,在堆栈溢出一些)在C#中获得一个值代表当前迭代的foreach循环? 例如,我现在根据具体情况做这样的事情: int i=0; foreach (Object o in collection) { // ... i++; } foreach用于迭代实现IEnumerable集合。 它通过调用集合上的GetEnumerator来完成此操作,该集合将返回一个Enumerator 。 这个枚举器有一个方法和一个属性: 的MoveNext() 当前
This came to my mind after I learned the following from this question: where T : struct We, C# developers, all know the basics of C#. I mean declarations, conditionals, loops, operators, etc. Some of us even mastered the stuff like Generics, anonymous types, lambdas, LINQ, ... But what are the most hidden features or tricks of C# that even C# fans, addicts, experts barely know? Here are
在我从这个问题中得知以下内容后,我想起了这件事: where T : struct 我们,C#开发人员,都知道C#的基础知识。 我的意思是声明,条件,循环,操作符等。 我们中的一些人甚至掌握了泛型,匿名类型,lambda,LINQ等... 但是C#中最隐藏的特性或技巧是什么?甚至连C#粉丝,瘾君子,专家都不知道? 以下是迄今为止显示的功能: 关键词 Michael Stum的yield var by Michael Stum using() kokos的using()语句 r
I was answering a question about the possibility of closures (legitimately) extending object-lifetimes when I ran into some extremely curious code-gen on the part of the C# compiler (4.0 if that matters). The shortest repro I can find is the following: Create a lambda that captures a local while calling a static method of the containing type. Assign the generated delegate-reference to an in
我回答了关于在C#编译器(如果重要的时候,4.0)遇到一些非常好奇的代码的情况下,关闭(合法地)延长对象生命期的可能性的问题。 我能找到的最短的repro如下: 在调用包含类型的静态方法时创建一个捕获本地的lambda。 将生成的委托引用分配给包含对象的实例字段。 结果:编译器创建一个闭包对象,该闭包对象引用创建lambda的对象,当它没有理由时 - 委托的“内部”目标是静态方法,并且lambda创建对象的实例成员不需要当