Guide to the Language
Download 2 Mb. Pdf ko'rish
|
C sharp
- Bu sahifa navigatsiya:
- Line Directive
- Region Directives
- CHAPTER 26 Delegates
Diagnostic Directives
There are two diagnostic directives: #error and #warning. The #error directive is used to abort a compilation by generating a compilation error. This directive can optionally take a parameter that provides an error description. #if Professional && Enterprise #error Build cannot be both Professional and Enterprise #endif Similar to error, the #warning directive generates a compilation warning message. This directive will not stop the compilation. #if !Professional && !Enterprise #warning Build should be Professional or Enterprise #endif ChaptEr 25 prEproCESSorS 144 Line Directive Another directive that affects the compiler’s output is #line. This directive is used to change the line number and optionally the source filename that is displayed when an error or warning occurs during compilation. This is mainly useful when using a program that combines the source files into an intermediate file, which is then compiled. #line 500 "MyFile" #error MyError // MyError on line 500 Region Directives The last two directives are #region and #endregion. They delimit a section of code that can be expanded or collapsed using the outlining feature of Visual Studio. #region MyRegion #endregion Just as the conditional directives, regions can be nested any number of levels deep. #region MyRegion #region MySubRegion #endregion #endregion ChaptEr 25 prEproCESSorS 145 © Mikael Olsson 2020 M. Olsson, C# 8 Quick Syntax Reference, https://doi.org/10.1007/978-1-4842-5577-3_26 CHAPTER 26 Delegates A delegate is a type used to reference a method. This allows methods to be assigned to variables and passed as arguments. The delegate’s declaration specifies the method signature to which objects of the delegate type can refer. Delegates are by convention named with each word initially capitalized, followed by Delegate at the end of the name. delegate void MyDelegate(string str); A method that matches the delegate’s signature can be assigned to a delegate object of this type. class MyClass { static void Print(string s) { System.Console.WriteLine(s); } static void Main() { MyDelegate d = Print; } } 146 This delegate object will behave as if it were the method itself, regardless of whether it refers to a static or an instance method. A method call on the object will be forwarded by the delegate to the method, and any return value will be passed back through the delegate. MyDelegate d = Print; d("Hello"); // "Hello" The syntax used here to instantiate the delegate is actually a simplified notation that was introduced in C# 2.0. The backward compatible way to instantiate a delegate is to use the regular reference type initialization syntax. MyDelegate d = new MyDelegate(Print); Download 2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling