0% found this document useful (0 votes)
29 views

C# Classes Syntax Steps

The document defines an Animal base class with properties like canSwing, groupOrClass, canFly, and fertilization. It then defines a Bird class that derives from Animal and adds properties for beakShape, nesting, numSpecies, and migration. The Main method creates a Bird object, sets its property values, and stores it in an array along with a Reptile object. It then iterates through the array and prints the type name and property values of each object.

Uploaded by

Abdiel Corro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

C# Classes Syntax Steps

The document defines an Animal base class with properties like canSwing, groupOrClass, canFly, and fertilization. It then defines a Bird class that derives from Animal and adds properties for beakShape, nesting, numSpecies, and migration. The Main method creates a Bird object, sets its property values, and stores it in an array along with a Reptile object. It then iterates through the array and prints the type name and property values of each object.

Uploaded by

Abdiel Corro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

class Program

{
static void Main(string[] args)
{
// STEP 8 : Create an OBJECT of the Bird class (in this case using dot notation)
Bird falcon = new Bird();

// STEP 9 : give values to the BIRD members


falcon.beakShape = "raptorial";
falcon.nesting = "scrapes";
falcon.numSpecies = 19;
falcon.migration = true;

// ANOTHER CLASS OBJECT I commented for example purposes using object initializer
// Reptile turtle = new Reptile() { numSpecies = 356, reproduction = "eggs",
//isDangerous = false, canRegenerate = false};

// STEP 10 : in this case it is printing the info. Here we create an array and a
// foreach loop to be able to iterate to each member, property of the two classes
// the bird class and the reptile class (this one is commented).

var animalInventory = new Animal[] { falcon, turtle };

foreach (var item in animalInventory)


{
Console.WriteLine(item.GetType().Name);
Console.WriteLine($"Can swing: {item.canSwing}");
Console.WriteLine($"Animal group: {item.groupOrClass}");
Console.WriteLine($"Can Fly: {item.canFly}");
Console.WriteLine($"Fertilization: {item.fertilization}");
Console.WriteLine();
}
}
}
// STEP 1 : Create the BASE class
public class Animal
{
// STEP 2: Creating a CONSTRUCTOR (in windows we need to create it manually)
public Animal() //how do I know? It looks like a method without the return type and
{ // it has the SAME name that its class “Animal”.
}

// STEP 3 : Give this class MEMBERS (in this case, members that all Animals have in common)
public bool canSwing { get; set; } // these are properties because get & set keywords
public string groupOrClass {get; set; } // these are properties
public bool canFly { get; set; } // these are properties
public string fertilization { get; set; } // these are properties
}

// STEP 4 : Create the DERIVED class


public class Bird : Animal
{
// STEP 5 : Create a CONSTRUCTOR for this class
public Bird()
{
// STEP 7 : initialize the BASE members in the derivate class
canSwing = false;
groupOrClass = "neornithes";
canFly = true;
fertilization = "internal";
}

// STEP 6 : Give this class MEMBERS


public string beakShape { get; set; }
public string nesting { get; set; }
public int numSpecies { get; set; }
public bool migration { get; set; }
}

You might also like