You are on page 1of 14

C# 7 and .Net Core 2.

Classes and Structs

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

Setter and Getter;

Property syntax;

Using keywords (get and set)


Auto Property

Using access modifier;

Using LAMDA expression;=> Later

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

 Structs are passed by value, where Classes are passed by reference:


 Passed by value means the change of the value of the parameter inside the method doesn’t
affect the value of the parameter variable outside the method.
 Passed by reference is the opposite.

 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

 The keyword partial:


 The partial keyword allows the class, struct, method, or interface to span multiple files.

 What is applied in one partial it applied to all:


 Keywords like (public, internal)
 Generic constraints like Attributes
Static keyword
The keyword static is used to overcome the need for initialization, and make the code
available for use instantly.
Uses of static keyword:
When referencing to another static classes : using static System.Console;
When creating a class;
the static class can be used with out initialization, the class should have static members.
When defining a field, property or even a function of a class (either the class is static or not).
Also with constructors: the static constructor is called only once when class is loaded and it used for
initializing static members; and also it doesn’t have access modifiers.
 Extension Methods:

 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?

You might also like