Professional Documents
Culture Documents
Ateik Alzehla
Content
Anonymous Types, readonly, const
Arguments
Calling Constructors
Overloading and Overriding Methods
Properties
Struct
Passing Parameters
Static keyword
Partial keyword
var, readonly, const and static
When using keyword var we create what so called an Anonymous Type. The
type is inferred from the initialization.
With readonly : the value of the variable can be changed only in the
constructor for initialization.
Keyword const cannot be changed at all
Arguments
Named Arguments
Optional Arguments
Should be at last
Named Argument is useful here.
Variable Number Of Arguments
The keyword params
Calling Constructors
There are many ways to call constructors:
By default when initializing the class with keyword new
By constructors that call another constructor in the same class, when the
class we has multiple constructors
Using base keyword
Using reflection.
Overloading and Overriding Methods
Overloading characteristics:
Same method name
Different parameter types.
And also different parameter numbers.
But not the return type.
It’s also not sufficient to differ by parameter names.
Overriding method:
using the keyword override, to replace the body of parent class’s method with new code;
Almost used when the parent method or even property is declared as virtual
Operator can be overriding=> this will be discussed later.
Properties
Properties
Property syntax;
Indexed Properties:
used with collections and uses this keyword
Struct
It’s syntax same as Class syntax, in general, we just replace the keyword
class with the keyword struct.
It’s also like Class, it can have Contractors, Methods, Properties, Fields.
But it’s not a class, It’s a Value Type whereas class is a Reference Type.
What does that means?
It can’t be derived (i.e. there is no Inheritance with structs)
So, Why structs?
Passing Parameters
For reference type parameters, a new initialization inside the method create a new reference.
With struct it doesn’t matter, Why?
Keyword ref:
Used to pass struct by value; (reference to value)
Used to pass reference to the class reference (reference to reference)
What will happen if we reinitialize the class inside the method?
Passing Parameters (Continued)
Keyword in:
Tomake the ValueType parameter read only; so you can’t reinitialize it or change its
content;
ForReferenceType parameters, the content can be changed but reinitialization is not
allowed
Keyword out:
An extra output from methods;
The parameter Should be assigned inside the method.
There are new ways to get more result from methods => later;
More Keywords
Along with this keyword, static keyword can be used to declare an extension method.
It Extend the class to have methods that doesn’t declared inside it.
It ‘s useful for reducing code and make these method available and easy to reach.
It’s very important to know about Extension methods, it’s widely used.
Creating classes
public class MyList public object this[int i]
{ {
private object[] _items; get
public MyList():this(0) {
{ return _items[i];
}
} set
public MyList(int capacity) {
{ _items[i] = value;
_items = new object[capacity]; }
} }
public void Add(object item) public int Count { get=> _items.Length; }
{ public IEnumerator GetEnumerator()
object[] temp = new object[Count + 1]; {
for (int i = 0; i < Count; i++) for (int i = 0; i < Count; i++)
{ {
temp[i] = this[i]; yield return _items[i];
} }
temp[Count] = item; }
_items = temp;
}
More questions?