Главная | Обратная связь | Поможем написать вашу работу!
МегаЛекции

Interface. Type Casting. Delegates. Modifiers. Accessibility Levels




Interface

Reference:

○ Unity Interface Tutorial:
https: //learn. unity. com/tutorial/interfaces#

○ Microsoft Documentation:
https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/interface

Not a Type: An interface is not a type, but it is used by (implemented by) both Classes and Structs

Beyond Virtual: An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition

Creating an Interface: Use capital “I” to begin name, specify Method Signatures only

interface IinterfaceName< Optional T>
       void MethodSignatureName(Type variable);
      void SignatureName(< Optional T> variable);

Implementation: Specify the Interfaces used following the class inheritance:
class ClassName: MonoBehaviour, IinterfaceName< Optional T>

Multiple Implementation: Unlike class derivation, a class may implement as many interfaces as you wish. While a class does not truly “derive” from an Interface, it is still considered an Instance of any Interface it implements

Common Interfaces:

IEnumerable: Something we can “step through, one item at a time”.

Examples of Implementation: Pretty much every Collection in. Net (such as List)implements IEnumerable.

IComparable: The data must have some concept of “before / after” or “greater than / less than”

Examples of Implementation: Most of the built-in. Net Types like “int”, “string”, etc.

IEquatable: Whether two objects are equivalent to each-other.

Type Casting

Delegates

Dynamic:

References:

JeremyBytes - Lambdas & LINQ in C# - Bonus: Deciphering the Join Method: An example of dynamic in practice to create an anonymous type as the return value from a function
https: //www. youtube. com/watch? v=tzR2qY6S4yw

Microsoft Documentation:
https: //docs. microsoft. com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic

Anonymous Type: dynamic is the type assigned to a variable whose type is anonymous.

Creating an Anonymous Type:

Dynamic: The most basic way of declaring a dynamic variable is with the “dynamic” keyword:
dynamic d = data;

■ “d” is created as an anonymously typed variable storing whatever “data” is

Multiple data {}: An anonymously typed variable can hold the value of multiple pieces of data when created with {} syntax:

                   dynamic d = new {Mathf. Epsilon, Color. black, “Hello”, 1};

Allowed, not allowed: “Anonymous type members must be declared with a member assignment, simple name, or member access” this means trying to insert a value type like “Hello” or “1” directly will not work because, think about it, how would you even access such a property off the variable “d”? Contrast this with the following statement, which is allowed:
dynamic d = new {Mathf. Epsilon, hello = " Hello" };

Make a name for yourself: hello now holds reference to the string “Hello” and so can be added to the anonymous type. This string can now be accessed by typing:
D. hello

■ Or just nest dynamics:

Push Errors to Compile Time:

You probably have this property: Dynamic variables might contain anything, as the following code demonstrates:
dynamic d1 = 1;

dynamic d2 = new {d1, Mathf. Epsilon, Color. black};

■ “d2” is created as an anonymously typed variable storing d1, Mathf. Epsilon, and Color. black.

○ Now that “d2” contains a float called “Epsilon” we can write the following code:

float toPrint = d. Epsilon;

Debug. Log(" toPrint: " + toPrint. ToString());

This makes no Intellisense: Note that in typing out “d2. Epsilon” Intellisense has no idea what is contained in d, it just assumes d has something in it called “Epsilon” and will try to return that value to be stored in “ToPrint”.

Return dynamic:

New {}: A method can return something of type “dynamic” in the way it can return something of any other type:
public dynamic DynamicMethod() {

return new {Mathf. Epsilon, hello = " Hello" };

}

Stored in var:

Modifiers

Accessibility Levels

https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/accessibility-levels

Public:

Anyone, anytime: Access is not restricted

○ Any Method or Member variable of a class can be accessed (read from) and modified (written to) freely by any other class

https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/public

Protected:

Only me and my Children: Access is limited to the containing class or types derived from the containing class.

○ If a parent (base) class has a “protected” member variable, a child (derived) class can access the variable

https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/protected

Internal:

Only me and my buddies: Access is limited to the current assembly

○ A group of classes all compiled as part of an assembly whose members are expected to communicate with one-another, but do not share information outside their framework

Assembly: A building-block of. NET, a. dll or. exe which contains source code

Protected Internal: Access is limited to the current assembly or types derived from the containing class

■ A “combination” access Modifier whose member variables can only be accessed by derived classes (like a regular protected class) while also having the properties of being internal

https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/internal

Private:

Only me: Access is limited to the containing type

○ Any Method or Member variable can only be read from / written to within the class in which it was declared.

Nested Access: A class whose definition is nested within the body of another class can still access private members

Private Protected: Access is limited to the containing class or types derived from the containing class within the current assembly

■ A “combination” access Modifier whose member variables can only be accessed by derived classes (like a regular protected class) while also having the properties of being private

https: //docs. microsoft. com/en-us/dotnet/csharp/language-reference/keywords/private

Поделиться:





Воспользуйтесь поиском по сайту:



©2015 - 2024 megalektsii.ru Все авторские права принадлежат авторам лекционных материалов. Обратная связь с нами...