Lecture 06 - programming fundamentals
Lecture 06 - programming fundamentals
• They are commonly used when you want to define a template for a
group of subclasses that share some common implementation
code, but you also want to guarantee that the objects of the
superclass cannot be created.
• For instance, let's say you need to create Dog, Cat and Fish objects.
They possess similar properties like color, size, and number of legs
as well as behavior so you create an Animal superclass. However,
what color is an Animal? How many legs does an Animal object
have? In this case, it doesn't make much sense to instantiate an
object of type Animal but rather only its subclasses.
sealed Class
• Use to restrict the inheritance feature of OOP.
• The sealed keyword tells the CLR that there is no class further
down to look for methods, and that speeds things up.
• namespace consoleprogram18
• {
• public sealed class Employee
• {
• public double empid;
• public string name;
• public int age;
•
• }
• public class salesrep:Employee
• {
• public int salary { get; }= 100000;
• Interfaces are more flexible than base classes because you can
define a single implementation that can implement multiple
interfaces.
➢The lowest address corresponds to the first element and the highest address to
the last element.
➢ To declare an array in C# :
• datatype[] arrayName;
• Eg: int [] balance;
//Initializing Array
num[0] = 1;
num[1] = 5;
num[2] = 75;
num[3] = 57;
num[4] = 88;
num[5] = 45;
• //traversal
• for (int i = 0; i < 3; i++)
• {
• for (int j = 0; j < 3; j++)
• {
• Console.Write(arr[i, j] + " ");
• }
• Console.WriteLine();//new line at each row
• Console.ReadKey();
• }}}}
Console program 26.1 – multidimensional array- without array size and operator
• using System;
• public class MultiArrayExample
• {
• public static void Main(string[] args)
• {
• int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };//declaration and initialization
•
• //traversal
• for(int i=0;i<3;i++){
• for(int j=0;j<3;j++){
• Console.Write(arr[i,j]+" ");
• }
• Console.WriteLine();//new line at each row
• Console.ReadKey();
• } } }
•
•
Jagged Arrays
• In C#, jagged array is also known as "array of arrays" because its elements
are arrays. The element size of jagged array can be different.
• Declaration of Jagged array – With two elements
int[][] arr = new int[2][];
• namespace Consoleprogram27
• {
• internal class Program
• {
• static void Main(string[] args)
• {
• int[][] arr = new int[2][];// Declare the array
• arr[0] = new int[] { 11, 21, 56, 78 };// Initialize the array
• arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };
• }
• Console.ReadKey();
• }}}