Guide to the Language
Download 2 Mb. Pdf ko'rish
|
C sharp
- Bu sahifa navigatsiya:
- Constant Guideline
- CHAPTER 30 Asynchronous Methods
In Parameters
Similar to the ref parameter modifier, C# 7.2 added the in modifier, which provides the ability to pass an argument as a readonly reference. Any code in the method that attempts to modify an in parameter (or its members in the case of a struct) will fail at compile time and so the parameter must be initialized prior to the method call. class MyApp { static void Test(in int num) { // num = 5; // error: readonly parameter } static void Main() { int i = 10; Test(i); // passed by readonly reference Test(2); // allowed, temporary variable created } } Chapter 29 Constants 175 Like the ref modifier, the in modifier prevents unnecessary copies from being made of value types. This is useful for performance reasons, particularly when passing a large struct object to a method that’s called multiple times. Constant Guideline In general, it is a good idea to always declare variables as const or readonly if they do not need to be reassigned. This ensures that the variables will not be changed anywhere in the program by mistake, which in turn helps to prevent bugs. It also clearly conveys to other developers when a variable is intended not to be modified. Chapter 29 Constants 177 © Mikael Olsson 2020 M. Olsson, C# 8 Quick Syntax Reference, https://doi.org/10.1007/978-1-4842-5577-3_30 CHAPTER 30 Asynchronous Methods An asynchronous method is a method that can return before it has finished executing. Any method that performs a potentially long-running task, such as accessing a web resource or reading a file, can be made asynchronous to improve the responsiveness of the program. This is especially important in graphical applications, because any method that takes a long time to execute on the user interface thread will cause the program to be unresponsive while waiting for that method to complete. 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