Session 3
Session 3
PROGRAMMING II
Slide 4
Inheritance in Action
public class A
{ public class AccessExample
private int _value = 10;
{
public static void Main(string[] args)
public class B : A
{ {
public int GetValue() var b = new A.B();
{ Console.WriteLine(b.GetValue());
return _value; }
} }
}
}
public class C : A
{
// public int GetValue()
// {
// return _value;
// }
}
Slide 6
Overriding Inherited Members
public class A
{
public virtual void Method1()
{
// Do something.
}
}
public class B : A
{
public override void Method1() {
// Do something else.
}
}
Slide 7
override Modifier
• The override modifier is required to extend or
modify the abstract or virtual implementation of an
inherited method, property, indexer, or event.
Slide 8
override Modifier
abstract class Shape
{
public abstract int GetArea();
}
Slide 9
Override Example
public class Employee
{
public string Name { get; }
Slide 10
Override Example contd.
public class SalesEmployee : Employee
{
private decimal _salesbonus;
Slide 12
Abstract Classes and Class Members
• The abstract keyword enables you to create classes
and class members that are incomplete and must be
implemented in a derived class.
Slide 13
Interfaces
• An interface defines a contract.
• A class or struct that implements an interface shall
adhere to its contract.
• An interface may inherit from multiple base
interfaces, and a class or struct may implement
multiple interfaces.
Slide 14
Interfaces
interface ISampleInterface
{
void SampleMethod();
}
}
Slide 15
Interfaces
public interface INamed
{
public string Name {get; set;}
}
• An interface may include
– Constants
– Static fields, methods, properties, indexers, and events
– Operators
– Nested Types
– Explicit access modifiers (default is public)
Slide 16
Interfaces
interface ISampleInterface
{
void SampleMethod();
}
}
Slide 17
Interface Inheritance
public interface IControl
{
void Paint();
}
public interface ISurface
{
void Draw();
}
public class SampleClass : IControl, ISurface
{
public void Paint()
{
Console.WriteLine("Paint method in SampleClass");
}
Slide 18
Interface Inheritance
public interface IControl
{
void Paint();
}
public interface ISurface
{
void Paint();
}
public class SampleClass : IControl, ISurface
{
public void IControl.Paint()
{
Console.WriteLine("Paint method from IControl");
}
Slide 19
Operator Overloading
• A user-defined type can overload a predefined C#
operator
• Use the operator keyword to declare an operator.
• An operator declaration must satisfy the following
rules:
– It includes both a public and a static modifier.
– A unary operator has one input parameter.
– A binary operator has two input parameters.
• In each case, at least one parameter must have type T or T?
where T is the type that contains the operator declaration.
Slide 20
Overloadable Operators
Slide 21
Operator Overloading
public class Fraction
{
private readonly int num;
private readonly int den;
Slide 22
Structs
• Structs are similar to classes in that they represent
data structures that can contain data members and
function members.
• A structure type (or struct type) is a value type that
can encapsulate data and related functionality.
• Structs are particularly useful for small data
structures that have value semantics.
– Complex numbers, points in a coordinate system, or key-
value pairs in a dictionary are all good examples of structs.
Slide 23
Structs
public struct Coords
{
public Coords(double x, double y)
{
X = x;
Y = y;
}
Slide 24
readonly Struct
• You use the readonly modifier to declare that a
structure type is immutable.
• All data members of a readonly struct must be read-
only as follows:
• Any field declaration must have the readonly
modifier
• Any property, including auto-implemented ones,
must be read-only
Slide 25
readonly Struct
public readonly struct Coords
{
public Coords(double x, double y)
{
X = x;
Y = y;
}
Slide 26
Records
• Beginning with C# 9, you use the record modifier to
define a reference type that provides built-in
functionality for encapsulating data
• C# 10 allows the record class syntax as a synonym to
clarify a reference type, and record struct to define a
value type with similar functionality
• When you declare a primary constructor on a record,
the compiler generates public properties for the
primary constructor parameters
– The primary constructor parameters to a record are referred
to as positional parameters.
Slide 27
Records and Record Class
//Record
public record Person(string FirstName, string
LastName);
//Record Class
public record Person
{
public required string FirstName { get; init; }
public required string LastName { get; init; }
};
Slide 28
Record Struct Types
public readonly record struct Point(double X, double
Y, double Z);
Slide 29
Sealed Modifier
When applied to a class, the sealed modifier prevents
other classes from inheriting from it.
In the following example, class B inherits from class A,
but no class can inherit from class B.
class A {}
sealed class B : A {}
Slide 30