Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ex01_Dog_class_example
- {
- // Class declaration
- public class Dog
- { // Opening brace of the class body
- // Field declaration
- private string name;
- // Constructor declaration
- public Dog()
- {
- this.name = "Balkan";
- }
- // Another constructor declaration
- public Dog(string name)
- {
- this.name = name;
- }
- // Property declaration
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- // Method declaration
- public void Bark()
- {
- Console.WriteLine("{0} said: Wow-wow!", name);
- }
- } // Closing brace of te class body
- class Program
- {
- static void Main()
- {
- string firstDogName = null;
- Console.WriteLine("Write first dog name: ");
- firstDogName = Console.ReadLine();
- // Using a constructor to create a dog with specified name
- Dog firstDog = new Dog(firstDogName);
- // Using a constructor to create a dog with a default name
- Dog secondDog = new Dog();
- Console.WriteLine("Write second dog name: ");
- string secondDogName = Console.ReadLine();
- // Using property to set the name of the dog
- secondDog.Name = secondDogName;
- // Creatin a dog with a default name
- Dog thirdDog = new Dog();
- Dog[] dogs = new Dog[] { firstDog, secondDog, thirdDog };
- foreach (Dog dog in dogs)
- {
- dog.Bark();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement