C# Programming Session2
C# Programming Session2
More C#
The plan
• Inheritance, interfaces
• Method overriding, virtual & override
• Delegates
• Lambda expressions
• Predicates
• Nullable
Inheritance
• A class can extend another class
• A class can extend a maximum of one class
• A class can implement any number of interfaces
Just
like
in Ja
va
Inheritance Separated by
colon
Java: super
Derived class
Super class C#: base
Constructor
in sub-class
Interfaces,
Super class Interfaces
notice initial ‘I’
Naming Convention
All interfaces in .NET start with an I
Interfaces
Create
new
instance
How to use a method as argument Result: Hello world is
printed to console
Calling this
method
With this
method as
argument
How to use a method as argument
Hello world
Hello students
Hello moon
Delegates
• Delegate is the “super class”
• Action is a specific type of delegate
• Return type is void
• It takes arguments according to the type parameters: <string, int, Person, …>
• Func<arg1, arg2, …, argX, return-type>
• Also a specific type of delegate
• Last type parameter is the return type, i.e. not void.
• First type parameters are arguments.
• We can define our own delegates.
Syntax to define a delegate
Defining my own delegate type:
MyDelegate
What am I
doing here? If(OnTimeChange != null) {
OnTimeChange.Invoke(s);
}
Adding methods to the delegate
Instance of the
class with the
delegate
Adding a
method to the
delegate
Alternatives:
Running the example
• An Action or Delegate is a list of methods
• Invoking the Action/Func/Delegate means to call all methods in the list
The plan
• Inheritance, interfaces
• Method overriding, virtual & override
• Delegates
• Lambda expressions
• Predicates
• Nullable
Lambda expression
• It’s a syntax to create delegates.
• It is an anonymous function
Read more here
• Examples:
Lambda expression No
arguments
private void Print() public void RunExample()
{ {
Console.WriteLine("Hello world"); Print();
} Action p = () => Console.WriteLine("Hello World");
p1.Invoke("Goodbye world");
}
Lambda • Creating a variable of type Func
Lambda example
Predicates (a method that defines a criteria, returns bool)
public void Run() Predicate public class BookRepository
{ {
List<Book> books = new BookRepository().GetBooks(); public List<Book> GetBooks()
{
List<Book> cheapBooks = books.FindAll(CheaperThan50Dollars); return new()
{
List<Book> alsoCheap = books.FindAll(book => book.Price < 50); new Book() {Title = "First", Price = 42},
} new Book() {Title = "Second", Price = 1337},
new Book() {Title = "Third", Price = 12.34m},
private bool CheaperThan50Dollars(Book obj) new Book() {Title = "Fourth", Price = 65.95m}
{ };
return obj.Price < 50; }
} Same result: Find }
all books, where
given a book, the
books price is less
than 50
Predicates (a method that defines a criteria, returns bool)
• Alternatively:
• Instantiate the property public string Description { get; set; } = null!;
• Or create a constructor
• This results in the property
never being null public string Description { get; set; } = "";
Not always
possible with
other types.
public string Description { get; set; }
Suppress null
• We use ”!” warning
bool? b = null;
• Can be done for all simple types and objects; append with ? to allow the
variable value to be null
Exercises