A class is a code template for creating objects.
It can contain:
Simplest class ever:
1: 2: 3: 4: 5: 6: 7: 8: |
|
In order to give birth to our Kottan (class) we need to use new
operator:
Cannot be inherited
Why do we need this?
Convenient way to create not reusable object
1:
|
|
Keywords to declare the accessibility of a type or member
Keyword |
ApplicableTo |
Description |
---|---|---|
private |
Member |
Access limited to the class |
protected |
Member |
Access limited to the class or derived classes |
internal |
Class, Member |
Access limited to the current assembly |
Keyword |
ApplicableTo |
Description |
---|---|---|
protected internal |
Member |
Access limited to current assembly and derived types |
public |
Class, Member |
No restrictions |
Therefore the following declaration:
1: 2: 3: 4: |
|
is equivalent to:
1: 2: 3: 4: |
|
Methods define behavior
The signature of every method consists of:
1: 2: 3: 4: 5: 6: 7: |
|
Constructor is meant to do all necessary for object creation
Two types of constructors:
Static
Runs once per type
Note: can be triggered by accessing a static member
Instance
Runs once per instance
These two guys are equivalent:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: |
|
params
keyword means that you can pass variable number of arguments
Just like ref but: - Used to pass data from inside the method back to calling code - Must be assigned before going OUT of method
kat = new Kottan()
Fields are named storage for values or references depending on their type.
Depending on where are they stored they could be:
this
is the reference to current instance
Can be used only inside instance context, never in static.
const
– set at compile time and cannot be change thereafter
enum
)Compiler can assume that value really will never change and copy it as a literal. Therefore if somebody referenced your assembly – the don’t get your changes until recompilation.
readonly – field can be set only* during construction.
Just get
and set
methods
interface
Syntactic sugar
1: 2: 3: 4: 5: 6: |
|
Syntactic sugar
1: 2: 3: 4: 5: 6: |
|
Property that takes one or more arguments and accessed with array-like syntax.
Simplify setting of accessible fields or properties
1: 2: 3: 4: 5: |
|
1: 2: 3: 4: 5: |
|
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: |
|
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: |
|
Operator |
IL method |
---|---|
+ |
op_UnaryPlus |
- |
op_UnaryNegation |
! |
op_LogicalNot |
Operator |
IL method |
---|---|
~ |
op_OnesComplement |
++ |
op_Increment |
-- |
op_Decrement |
Operator |
IL method |
---|---|
+ |
op_Addition |
- |
op_Subtraction |
* |
op_Multiply |
/ |
op_Division |
% |
op_Modulus |
Operator |
IL method |
---|---|
^ |
op_ExcusiveOr |
& |
op_BitwiseAnd |
| | op_BitwiseOr |
<< | op_LeftShift |
Operator |
IL method |
---|---|
>> | op_RightShift |
== |
op_Equality |
!= |
op_Inequality |
< | op_LessThan |
Operator |
IL method |
---|---|
> | op_GreaterThan |
<= | op_LessThanOrEqual |
>= | op_GreaterThanOrEqual |
Implicit - casting to other type without specifying it
1:
|
|
Explicit - casting to other type only explicitly specifying it
1:
|
|
Conversion operators are not widespread because in most cases it’s not obvious that types support them.
More preferred approaches, that easier to discover:
Member that enables type to provide notifications when interesting things happen, using a subscription-based model*.
*Don’t go into much detail since we have a lecture about delegates and events
Just like the normal classes with few differences:
Destructors are for releasing unmanaged resources.
1: 2: 3: 4: 5: 6: 7: |
|
Similar to classes with the following differences:
When performance is critical:
Be aware that a namespace and an assembly aren’t necessarily related.
CLR doesn’t know anything about namespaces because it is just a way to make the type more unique.
A single assembly can contain types in different namespaces. For example, the System.Int32 and System.Text.StringBuilder types are both in the MSCorLib.dll assembly.
One namespace can be in several Assemblies, for example System.Configuration present in System.Windows.Form.dll and System.Data.dll
All of your classes and almost every classes in .NET are implicitly derived from System.Object class.
Therefore the following records are the same:
1: 2: 3: |
|
Equals() – override me
Returns true if two objects have the same value
GetHashCode() – override me
Returns a hash code for this object’s value
ToString() – override me
Returns name of the type (this.GetType().FullName)
GetType() – you don’t own me!
Returns an instance of a Type-derived object that identifies the type of the object.
*also there are Finalize() and MemberwiseClone()
Special type that lets you specify a group of named numeric constants
You can combine enum members. To prevent ambiguities values should be assigned in powers of two:
1: 2: 3: 4: 5: 6: 7: |
|
Then use bitwise operators in order to combine enums and determine if value exists in the combination:
1: 2: |
|
Interface is a group of related methods, properties and events