This shows you the differences between two versions of the page.
pjv:laboratoare:2023:02 [2023/10/23 09:39] alexandru.gradinaru |
pjv:laboratoare:2023:02 [2024/10/07 10:48] (current) alexandru.gradinaru |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Introducere in Scripting C# ===== | + | ===== 2. Introducere in Scripting C# ===== |
==== Cerinte ===== | ==== Cerinte ===== | ||
Line 70: | Line 70: | ||
A common example of the significance of this order occurs when a UnityScript file needs to reference a class defined in a C# file. To achieve this, you need to place the C# file inside a Plugins folder, and the UnityScript file in a non-special folder. Because C# code is compiled before JS code, so in general, while JS code can access C# classes, the opposite is not possible If you don't do this, an error is thrown saying that the C# class cannot be found. | A common example of the significance of this order occurs when a UnityScript file needs to reference a class defined in a C# file. To achieve this, you need to place the C# file inside a Plugins folder, and the UnityScript file in a non-special folder. Because C# code is compiled before JS code, so in general, while JS code can access C# classes, the opposite is not possible If you don't do this, an error is thrown saying that the C# class cannot be found. | ||
+ | |||
+ | === C# Basics === | ||
+ | |||
+ | > **Static Classes and Singleton** | ||
+ | |||
+ | Both can be invoked without instantiation, both provide only one "Instance" and neither of them is thread-safe. | ||
+ | |||
+ | A [static](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static) class is basically the same as a non-static class, but there is one difference: a static class contains only static members and a private constructor and cannot be instantiated. In other words, you cannot use the [new](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/new) keyword to create a variable of the class type. The design style embodied in a static class is purely procedural. | ||
+ | |||
+ | Singleton, on the other hand, is a pattern specific to OO design. It is an instance of an object (with all the possibilities inherent in that, such as polymorphism), with a creation procedure that ensures that there is only ever one instance of that particular role over its entire lifetime. | ||
+ | |||
+ | > **The Virtual keyword** | ||
+ | |||
+ | The **'virtual'** keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. By default, methods are non-virtual and you cannot override a non-virtual method. You cannot use the `virtual` modifier with the `static`, `abstract`, `private`, or `override`modifiers. | ||
+ | |||
+ | For example, this method can be overridden by any class that inherits it: | ||
+ | |||
+ | <code c#> | ||
+ | public virtual double Area() | ||
+ | { | ||
+ | return x * y; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | > **Abstract Classes and interfaces** | ||
+ | |||
+ | An **abstract class** is a special kind of class that cannot be instantiated. An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. | ||
+ | |||
+ | When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface. | ||
+ | |||
+ | When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared `abstract`. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes. | ||
+ | |||
+ | There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison: | ||
+ | |||
+ | | **Feature** | **Interface** | **Abstract class** | | ||
+ | | --- | --- | --- | | ||
+ | | **Multiple inheritances** | A class may inherit several interfaces. | A class may inherit only one abstract class. | | ||
+ | | **Default implementation** | An interface cannot provide any code, just the signature. | An abstract class can provide complete, default code and/or just the details that have to be overridden. | | ||
+ | | **Access Modfiers** | An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public | An abstract class can contain access modifiers for the subs, functions, properties | | ||
+ | | **Core VS Peripheral** | Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface. | An abstract class defines the core identity of a class and there it is used for objects of the same type. | | ||
+ | |||
+ | > **Methods and functions** | ||
+ | |||
+ | A **function** is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed. | ||
+ | |||
+ | A **method** is a piece of code that is called by a name that is associated with an object. In most respects it is identical to a function except for two key differences: | ||
+ | |||
+ | - A method is implicitly passed the object on which it was called. | ||
+ | - A method is able to operate on data that is contained within the class (remembering that an object is an instance of a class – the class is the definition, the object is an instance of that data). | ||
+ | |||
+ | > **Generic Functions and Generic Classes** | ||
+ | |||
+ | Generic classes encapsulate operations that are not specific to a particular data type. The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees, and so on. Operations such as adding and removing items from the collection are performed in basically the same way regardless of the type of data being stored. | ||
+ | |||
+ | Generic functions are similarly those which describe the algorithm without specifying a particular data type. | ||
=== Unity C# References === | === Unity C# References === |