C#-CAI Flashcards
internal class Book
//is internal so it cannot be access thought other projects
//new class named book
public string title;
public string description;
public int pages;
//define the variables/atributes of book class
public Book(string aTitle, string aAuthor, int aPages)
{
title = aTitle;
description = aAuthor;
pages = aPages;
}
// this constructor is gonna recived a title, this titile ll be the variable title.
Book myBook = new Book(“Sample Title”, “Sample Author”, 123);
create a new book instance, aka object
explain this
public string name;
public string description;
public Hobbies (string name, string description) { this.name = name; this.description = description; }
static void Main(string[] args) explain it
the entry point of a console application (or any .NET Core application that isn’t a library).
Main: This is the name of the method. The Main method is special because the .NET runtime looks for this method as the starting point of the application.
static meaning
static void Main(string[] args)
This keyword means that the method belongs to the type itself, rather than an instance of the type. In other words, you don’t need to create an object of the class to call a static method.
create a method for the class. Do I need to pass the props/atributes?
public bool isAvailable()
{
if (pages == 0)
{
return false;
}
return true;
}
what is a getter and a setter?
Using getters and setters, you can:
Validate data before it’s stored in an object.
Transform data before it’s returned or stored.
what is a constructor?
A constructor is a special method in a class that gets executed whenever an instance of the class is created. Constructors are used to initialize the object’s state, meaning its properties or fields, and to perform any setup or validation that’s required when the object is instantiated
Is getter and setter get called in the constructor?
In the constructor of the Person class, we’re using the Name property to set the name. This means that the setter for Name will be called when you create a new Person object using this constructor. The getter will only be called when you attempt to access the value of the Name property.
the constructor shouldent return something?
NO! The error you’re seeing is because constructors don’t have a return type, not even void. In C#, the casing (capitalization) of identifiers matters. If the constructor’s name doesn’t exactly match the class name (in terms of casing), the C# compiler thinks it’s a method, which is why you’re getting an error about returning something.
what is encapsulation?
The field _name is private, meaning it cannot be accessed directly outside of the TVShow class. Instead, we provide controlled access to that field via a public property, in this case Name.
underscore convetion means…
By convention, private fields sometimes start with an underscore.
purpose of return value of a property by the set/get by the Name public property that controls de private _name field/property of the object. Why?
You’re accessing the Name property of the show object, which internally retrieves the value from the _name field. This indirect access allows the class to enforce any rules or behaviors around getting or setting the value, instead of directly exposing the field.