在我从这个问题中了解到以下内容后,我想到了这一点:

where T : struct

我们,C#开发人员,都知道C#的基础知识。我指的是声明、条件、循环、运算符等。

我们中的一些人甚至掌握了Generics、匿名类型、lambdas、LINQ等等。。。

但是,即使是C#的粉丝、瘾君子和专家也几乎不知道C#最隐藏的功能或技巧是什么?

以下是迄今为止揭示的功能:

关键词

迈克尔·斯图姆的产量Michael Stum的varkokos的using()语句kokos只读由Mike Stone作者:Ed Swangren由Rocketpants改进因死亡而违约全球::由pzycomanAlexCuse的using()块Jakubšturc的挥发性Jakubšturc的外部别名

属性

Michael Stum的DefaultValueAttributeDannySmurf的ObsoleteAttribute调试器DisplayAttribute(按Stu)bdukes提供的DebuggerBrowseble和DebuggerStepThroughmarxidad的ThreadStaticAttributeMartin Clarke的FlagsAttributeAndrewBurns的ConditionalAttribute

语法

?? kokos的(合并空值)运算符Nick Berardi的数字标记其中T:Lars Mæhlum的新Keith的隐式泛型Keith的单参数lambdas基思汽车财产Keith的命名空间别名Patrick的带@的逐字字符串文字按lfoost列出的枚举值@marxidad的variableamesmarxidad的事件运算符由Portman设置字符串括号格式xanadot的属性访问器可访问性修饰符JasonS的条件(三元)运算符(?:)Binoj Antony检查和未检查操作员Flory的隐式和显式运算符

语言功能

Brad Barker的可空类型Keith的匿名类型__由Judah Himango制作的makeref __reftype __refvaluelomaxx的对象初始化器达科他州David的字符串格式marxidad的扩展方法Jon Erickson的部分方法John Asbeck的预处理器指令Robert Durgin的DEBUG预处理器指令SefBkn导致操作员过载通过chakrit推断类型Rob Gough将布尔运算符提升到下一级通过Roman Boiko将值类型变量作为不带装箱的接口传递由Roman Boiko编程确定声明的变量类型Chris的静态构造器使用LINQ by roosteronacid更容易进行眼睛/精简ORM映射__Zac Bowling的arglist

Visual Studio功能

Himadri在编辑器中选择文本块DannySmurf的片段

框架

KiwiCastard的TransactionScopeKiwiAstard的从属事务IainMH的<T>可为空迪亚戈的Mutex按ageektrapped列出的System.IO.PathJuan Manuel的WeakReference

方法和财产

KiwiAstard的String.IsNullOrEmpty()方法KiwiCastard的List.ForEach()方法Will Dean的BeginInvoke()、EndInvoke(()方法Nullable<T>。HasValue和Nullable<T>。Rismo的Value财产John Sheehan的GetValueOrDefault方法

提示和技巧

Andreas H.R.Nilsson的事件处理程序的好方法John的大写比较访问匿名类型而不通过dp进行反射Will延迟实例化集合财产的快速方法chosteronacid提供的类似JavaScript的匿名内联函数

另外

kokos的netmodulesDuncan Smart的LINQBridgeJoel Coehorn的并行扩展


当前回答

System.Diagnostics.Debug.Assert (false);

将触发一个弹出窗口,并允许您在执行期间将调试器附加到正在运行的.NET进程。对于某些原因无法直接调试ASP.NET应用程序的情况非常有用。

其他回答

这与静态构造函数有关。这是一种执行静态销毁的方法(即当程序退出时清理资源)。

第一堂课:

class StaticDestructor
{
    /// <summary>
    /// The delegate that is invoked when the destructor is called.
    /// </summary>
    public delegate void Handler();
    private Handler doDestroy;

    /// <summary>
    /// Creates a new static destructor with the specified delegate to handle the destruction.
    /// </summary>
    /// <param name="method">The delegate that will handle destruction.</param>
    public StaticDestructor(Handler method)
    {
        doDestroy = method;
    }

    ~StaticDestructor()
    {
        doDestroy();
    }
}

然后,作为类的成员,您希望使用“静态析构函数”:

private static readonly StaticDestructor destructor = new StaticDestructor
(
    delegate()
    {
        //Cleanup here
    }
);

现在,当最终垃圾收集发生时,将调用此函数。如果您确实需要释放某些资源,这很有用。

表现出这种行为的快速而肮脏的程序:

using System;

namespace TestStaticDestructor
{
    class StaticDestructor
    {
        public delegate void Handler();
        private Handler doDestroy;

        public StaticDestructor(Handler method)
        {
            doDestroy = method;
        }

        ~StaticDestructor()
        {
            doDestroy();
        }
    }

    class SomeClass
    {
        static SomeClass()
        {
            Console.WriteLine("Statically constructed!");
        }

        static readonly StaticDestructor destructor = new StaticDestructor(
            delegate()
            {
                Console.WriteLine("Statically destructed!");
            }
        );
    }

    class Program
    {
        static void Main(string[] args)
        {
            SomeClass someClass = new SomeClass();
            someClass = null;
            System.Threading.Thread.Sleep(1000);
        }
    }
}

当程序退出时,调用“静态析构函数”。

我倾向于发现大多数C#开发人员不知道“可空”类型。基本上,基元可以具有空值。

double? num1 = null; 
double num2 = num1 ?? -100;

将一个可为null的双精度值num1设置为null,然后将一个常规双精度值num 2设置为num1或-100(如果num1为null)。

http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx

关于Nullable类型还有一点:

DateTime? tmp = new DateTime();
tmp = null;
return tmp.ToString();

它是返回String.Empty。有关详细信息,请查看此链接

不可序列化的营销事件:

[field:NonSerializable]
public event SomeDelegate SomeEvent;

我喜欢几件事:

-如果创建类似于

 public interface SomeObject<T> where T : SomeObject<T>, new()

将从该接口继承的任何内容强制包含无参数构造函数。它对于我遇到过几件事。

-使用匿名类型动态创建有用的对象:

var myAwesomeObject = new {Name="Foo", Size=10};

-最后,许多Java开发人员熟悉语法,如:

public synchronized void MySynchronizedMethod(){}

然而,在C#中,这不是有效的语法。解决方法是方法属性:

 [MethodImpl(MethodImplOptions.Synchronized)]
 public void MySynchronizedMethod(){}

以下是一些有趣的隐藏C#特性,以未记录的C#关键字的形式出现:

__makeref

__reftype

__refvalue

__arglist

这些是未记录的C#关键字(甚至Visual Studio都能识别它们!),它们是为了在泛型之前更有效地装箱/拆箱而添加到的。它们与System.TypedReference结构协同工作。

还有__arglist,用于可变长度参数列表。

人们不太了解的一件事是System.WeakReference——一个非常有用的类,它可以跟踪对象,但仍然允许垃圾收集器收集它。

最有用的“隐藏”特性是yield return关键字。它并没有真正隐藏,但很多人都不知道它;它允许通过在后台生成状态机来延迟执行查询。Raymond Chen最近发布了关于内部细节的帖子。