Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Práctica: Constructores | Introduction to Object-Oriented Programming (OOP)
C# Beyond Basics

Práctica: ConstructoresPráctica: Constructores

Se da una clase simple llamada Dog. Crea un constructor que tome los argumentos nombre, raza, edad, e inicialice los campos a partir de los valores de los argumentos.

cs

index.cs

1. In order to assign the argument values to the fields without raising any error you'll have to use `this` operator, since the arguments are the same name as the fields.

        using System;

        class Dog {
            public string name;
            public string breed;
            public int age;
            
            // Write constructor code below this line
            public Dog(string name, string breed, int age) {
                this.name = name;
                this.breed = breed;
                this.age = age;
            }
            // Write constructor code above this line
            
            public void bark() {
                Console.WriteLine("Woof!");
            }
        }
        
        public class ConsoleApp
        {
            public static void Main(string[] args)
            {
                Dog dog = new Dog("Dobby", "Dobermann", 4);
                dog.bark();
            }
        }
    

¿Todo estuvo claro?

Sección 3. Capítulo 10
course content

Contenido del Curso

C# Beyond Basics

Práctica: ConstructoresPráctica: Constructores

Se da una clase simple llamada Dog. Crea un constructor que tome los argumentos nombre, raza, edad, e inicialice los campos a partir de los valores de los argumentos.

cs

index.cs

1. In order to assign the argument values to the fields without raising any error you'll have to use `this` operator, since the arguments are the same name as the fields.

        using System;

        class Dog {
            public string name;
            public string breed;
            public int age;
            
            // Write constructor code below this line
            public Dog(string name, string breed, int age) {
                this.name = name;
                this.breed = breed;
                this.age = age;
            }
            // Write constructor code above this line
            
            public void bark() {
                Console.WriteLine("Woof!");
            }
        }
        
        public class ConsoleApp
        {
            public static void Main(string[] args)
            {
                Dog dog = new Dog("Dobby", "Dobermann", 4);
                dog.bark();
            }
        }
    

¿Todo estuvo claro?

Sección 3. Capítulo 10
some-alt