Lecture No. 07
Dated: 29-04-2025
Auto Implemented Properties
using System;
public class Customer {
public int ID { get; set; }
public string Name { get; set; }
}
public class autoimplementedcustomermanager {
static void Main () {
Customer cust = new Customer();
cust.ID = 1;
cust.Name = "Amelio Rosales";
Console.WriteLine(
"ID: {0}, Name: {1}",
cust.ID,
cust.Name);
Console.ReadKey();
}
}
Indexer
using System;
class intindexer {
private string[] mydata;
public intindexer (int size) {
mydata = new string[size];
for (int i = 0; i < size; i++) {
mydata[i] = "empty";
}
}
public string this[int pos] {
get {
return mydata[pos];
}
set {
mydata[pos] = value;
}
}
static void Main (string[] args) {
int size = 10;
intindexer myind = new intindexer(size);
myind[9] = "Some Value";
myind[3] = "Another Value";
myind[5] = "Any Value";
Console.WriteLine("\nindexer Output\n");
for (int i = 0; i < size; i++) {
Console.WriteLine("myind[{0}]: {1}", i, myind[i]);
}
}
}
Output:
Myind[0]: empty
Myind[1]: empty
Myind[2]: empty
Myind[3]: Another Value
myind[4]: empty
Myind[5]: Any Value
Myind[6]: empty
Myind[7]: empty
Myind[8]: empty
Myind[9]: Some Value
Multiple Parameters
public object this[int param1, ..., int paramn]
{
get {
// process and return some class data
}
set {
// process and assign some class data
}
}
C#
built-in types are aliases for .NET Framework
types.
Structs
/// <summary>
/// Custom struct type, representing a rectangular shape
/// </summary>
struct Rectangle {
/// <summary>
/// Backing Store for Width
/// </summary>
private int m_width;
/// <summary>
/// Width of rectangle
/// </summary>
public int Width {
get {
return m_width;
}
set {
M_width = value;
}
}
/// <summary>
/// Backing store for Height
/// </summary>
private int m_height;
/// <summary>
/// Height of rectangle
/// </summary>
public int Height {
get {
return m_height;
}
set {
M_height = value;
}
}
}
using System;
/// <summary>
/// Example of declaring and using a struct
/// </summary>
class structexample {
/// <summary>
/// Entry point: execution starts here
/// </summary>
static void Main () {
// instantiate a new Rectangle struct
// where Width is set to 1 and Height is set to 3
Rectangle rect1 = new Rectangle();
rect1.Width = 1;
rect1.Height = 3;
// show the value of Width and Height for rect1
Console.WriteLine("rect1: {0}:{1}", rect1.Width, rect1.Height);
Console.ReadKey();
}
}
Output:
Rect1: 1:3
// you can also use object initialization syntax
Rectangle rect11 = new Rectangle {
Width = 1,
Height = 3
};
Interfaces
interface IMyInterface {
void MethodToImplement ();
}
using System;
class InterfaceImplementer : IMyInterface {
static void Main () {
InterfaceImplementer iimp = new InterfaceImplementer();
iimp.MethodToImplement();
}
public void MethodToImplement () {
Console.WriteLine("methodtoimplement() called.");
}
}
Inheritance
using System;
interface IParentInterface {
void ParentInterfaceMethod ();
}
interface IMyInterface : IParentInterface {
void MethodToImplement ();
}
class InterfaceImplementer : IMyInterface {
static void Main () {
InterfaceImplementer iimp = new InterfaceImplementer();
iimp.MethodToImplement();
iimp.ParentInterfaceMethod();
}
public void MethodToImplement () {
Console.WriteLine("methodtoimplement() called.");
}
public void ParentInterfaceMethod () {
Console.WriteLine("parentinterfacemethod() called.");
}
}