3. Creating Types In C# || C# 10 Flashcards
What is a ‘field’?
A field is a variable that is a member of a class or a struct:
~~~
class Octopus
{
string name;
public int Age = 10;
}
~~~
Describe readonly modifier
The readonly modifier prevents a field from being modified after construction. A read-only field can be assigned only on its declaration or within the enclosing type’s constructor.
Describe a constant
A constant is evaluated statically at compile time and the compiler literally substitutes its value whenever used
A constant can be any of the built-in types or an enum type.
Name a difference between readonly field and a constant
- Initialisation:
A. Read-only field can be initialised without an assigned value. Default value will be assigned automatically.
B. Constant must be initialised with a value. - Field can be of any type, while constant must be only of built-in or enum type.
What is a method?
A method performs a series of statements. A method can receive an input data from the caller by specifying a parameters, and an output data by specifying a return type or a void type.
What is method’s signature?
A method’s signature comprises its name and and parameters types in order (but not the parameters names, nor return type)
What is a expression-bodied method?
Instead of :int Foo(int x) { return x*2; }
Use:int Foo(int x) => x*2;
What is static local method?
Local method is a method within a method. And adding a static modifier prevents it from seeing local variables and parameters of enclosing method from any accidental reffering to them.
What is a constructor?
Constructors run initialization code in a class or struct. A constructor is defined like a method, except that the method name and return type are reduced to the name of the enclosing type
What is constructor overloading? How it is written?
One constructor can call another constructor using ‘this’ keyword:public Wine (decimal price, int year) : this(price) {Year= year;}
In this scenario second constructor is executed first
What is a deconstructor?
Describe syntax
Deconstructing method act as an opposite of constructor- assigns fields back to a set of variables
Deconstruct method must be called Deconstruct and have one or more out parameters
~~~
public void Deconstruct (out float width, out float height)
{
width= Width;
height= Height;
}
~~~
What is an object initialization?
To simplify objects initialization any accesible fields or properties can be set directly after constructor:
~~~
Bunny b1= new Bunny{ Name= “Bo”, LikesCarrots= true, LikesHumans= false};
~~~
What is a property?
A property is declared like a field but with a get/set block added
If you want to expose property as read-only to other types you can set a ‘set’ accesor to be private or protected or not set set accessor at all
What is a expression-bodied property?
You can write an expression with a fat arrow which replaces all the braces and return statements
~~~
public decimal Worth
{
get => currentPrice * sharesOwned;
set => sharesOwned = value / currentPrice;
}
~~~
What is a init-only setter?
Set accessor which lets you to set a property’s value when setting the property, but forbids you from modifying its value later on
~~~
{get; init;}
var note = new Note {Pitch= 50};
~~~
Then: note.Pitch = 200;
// throws error
What is a finalizer?
Finalizers are class-only methods that executes before the garbage collector reclaims the memory of unreferenced object. The syntax is name of the class prefixes with ~ symbol
~~~
class Class1{
~Class1(){…}
}
~~~
What is a partial type?
Partial types allow a type definition to be split - typically across multiple files.
Each participant must have the ‘partial’ declaration
Describe partial method
A partial method consist of two parts: a definition and an implementation.
Partial method must:
* be void and
* are implicitly private.
* They cannot include out parameters
What are extended partial methods?
They are designed for the reverse code generation scenario
A partial method declaration is extended if it begins with an accessibility modifier:
~~~
public partial class Test{
public partial void M1();
}
~~~
Extended partial methods must have an implementation
Why is the inheritance used?
A class can inherit from another class to extend or customise the original class. Inheriting from a class lets you reuse the functionality in that class instead of building it from scratch.
Class can inherit from only one class
What is a polymorphism?
Polymorphism means that all subclasses (those which inherit) have all the features of the base class but not the other way around
What is an upcasting?
An upcast operation creates a base class reference from a subclass reference.
Although two objects refer to the identical object, upcasted one has a more restrictive view on that object:
~~~
House h = new House();
Object o = h;
~~~Console. WriteLine(o.NumberOfWindows);
// throws compile error
What is a downcasting operation?
A downcasting operation creates a subclass reference from a base class reference:Stock stock= new Stock();
Asset a = stock;
// UpcastingStock s = (Stock)a;
// Downcasting
Only the references are affected to this change - not the underlying object. A downcast requires an explicit cast because it can potentially fail at runtime