Indexers. Selection statements. Iteration statements. Jump statements. Exception handling statements. Checked and unchecked
Indexers Indexers allow instances of a class or struct to be indexed just like arrays https: //docs. microsoft. com/en-us/dotnet/csharp/programming-guide/indexers/ Statement Keywords: https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/language-specification/statements Selection statements ● If ● Else Switch: ● Goto https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/language-specification/statements#the-switch-statement ● Case ● Switch based on Type: switch(shape) { case Circle c: WriteLine($" circle with radius {c. Radius}" ); break; case Rectangle s when (s. Length == s. Height): WriteLine($" {s. Length} x {s. Height} square" ); break; case Rectangle r: WriteLine($" {r. Length} x {r. Height} rectangle" ); break; default: WriteLine(" < unknown shape> " ); break; case null: throw new ArgumentNullException(nameof(shape)); } ● Iteration statements ● Do ● For ● Foreach ● In ● while Jump statements ● Break ● Continue ● Default ● Goto ● Return ● yield Exception handling statements ● Throw ● Try-catch ● Try-finally ● try-catch-finally Checked and unchecked ● Checked ● unchecked fixed statement ● lock statement https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/statement-keywords Properties, Fields, Variables, Argument Keywords Ideal number of Properties for a Method: 4 or less “In order to push method arguments onto the stack, compiled code has the following MSIL opcodes to choose from: ldarg. 0, ldarg. 1, ldarg. 2, ldarg. 3, ldarg. S, ldarg ldarg. 0 to ldarg. 3 is used to push the first 4 method arguments onto the stack (including this as the first argument for instance methods). ” https: //stackoverflow. com/questions/12658883/what-is-the-maximum-number-of-parameters-that-a-c-sharp-method-can-be-defined-as Properties ● Reference: ○ JeremyBytes - C# Properties: ● Properties: ○ Just Methods: Allow for the implicit creation of Get / Set Methods to control assignment of values to Private Variables or access to them. ● Backing Fields: ○ Literally a Field: Every Property needs a field to actually store its associated data. This data is stored in a field which is itself made private to restrict access to it from other classes. This field “backs” the Method and so is called a “Backing Field”
● Auto-Properties: ○ Implicit Backing Fields: To save you the trouble of typing out a backing field, you can use auto-properties which makes the Compiler create completely inaccessible fields to hold the value to be stored within the Property: ● Example: get {return _cash; } set { _cash = value; } } private float _cash; Out / In / Ref ● REF: ● IN: ● OUT: ○ Output Data: Change a Parameter in a Method to become the receptacle for data, separate from the Method’s return value ○ Define an out variable: The out keyword must be specified both in the Method Signature and when the Method is called. ■ Method Signature: void MethodName(out Type variableName) ■ Inline Method Call: Raycast(out RaycastHit hit) ■ Multi-Line Method Call: ○ Creating a Method which has an Out Parameter and a return type: Once an out parameter is declared, it must be assigned to by the method before the return statement is called ○ https: //www. geeksforgeeks. org/out-parameter-with-examples-in-c-sharp/ Operators: Ternary Operator? ● Essentially, this represents an abbreviated way of writing if-else statements ● The following code is identical in its function: ○ If (condition) { Value = 1f; } Else { ○ Value = condition? 1f: 0f; ● Example usage: In C#, bool cannot implicitly be converted to int, (let alone float) so the above provides a concise one-line example of setting “value” to either 1f or 0f depending on the evaluation of “condition” New Operator: ● Instantiate a type: The new operator creates a new instance of a type ● Invokes a Constructor: => Operator: ● Lambda operator: In lambda expressions, the lambda operator => separates the input variables on the left side from the lambda body on the right side. ● Expression body definition: ○ Defines a member through an expression: ■ Member: https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/operators/lambda-operator Types Class & Structs (Objects) ● Class: ○ Instantiated with new keyword: ● Struct: ● Class vs Struct: ○ Classes: A class is a reference type. When an object of the class is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data. ■ Used to model more complex behavior, or data that is intended to be modified after a class object is created ○ Structs: A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.
■ Best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created https: //docs. microsoft. com/en-us/dotnet/csharp/programming-guide/classes-and-structs/
Воспользуйтесь поиском по сайту: ©2015 - 2024 megalektsii.ru Все авторские права принадлежат авторам лекционных материалов. Обратная связь с нами...
|