Fields
This lesson will go into the details of the member variables or fields of a class.
We'll cover the following...
Fields #
As discussed earlier, fields are actually the member variables inside a class. For instance, in a class representing a vending machine, the VendingMachine
class might contain the following fields:
- A
count
of typeint
that stores the count of products available in the machine. - A
capacity
of typeint
that stores the maximum number of products the machine can have. - A
moneyCollected
of typeint
to store the money it has collected. - A
manufacturer
of typestring
to store the name of the manufacturer of the machine.
The fields in the VendingMachine
class could be defined like this:
Press + to interact
public class VendingMachine{private int _count;private int _moneyCollected;public int Capacity;public string Manufacturer;}
As it has been already mentioned that declaring public
fields is not a good idea when it comes to the security of the code ...