✅ ”base”

public class Person
{
protected string ssn = "444-55-6666";
protected string name = "John L. Malgraine";

public virtual void GetInfo()
{
Console.WriteLine("Name: {0}", name);
Console.WriteLine("SSN: {0}", ssn);
}
}
class Employee : Person
{
public string id = "ABC567EFG";
public override void GetInfo()
{
// Calling the base class GetInfo method:
base.GetInfo();
Console.WriteLine("Employee ID: {0}", id);
}
}

class TestClass
{
static void Main()
{
Employee E = new Employee();
E.GetInfo();
}
}
/*
Output
Name: John L. Malgraine
SSN: 444-55-6666
Employee ID: ABC567EFG
*/
public class Person
{
protected string ssn = "444-55-6666";
protected string name = "John L. Malgraine";

public virtual void GetInfo()
{
Console.WriteLine("Name: {0}", name);
Console.WriteLine("SSN: {0}", ssn);
}
}
class Employee : Person
{
public string id = "ABC567EFG";
public override void GetInfo()
{
// Calling the base class GetInfo method:
base.GetInfo();
Console.WriteLine("Employee ID: {0}", id);
}
}

class TestClass
{
static void Main()
{
Employee E = new Employee();
E.GetInfo();
}
}
/*
Output
Name: John L. Malgraine
SSN: 444-55-6666
Employee ID: ABC567EFG
*/
Is the “base” keyword only used to get the superclasses info?
19 Replies
Merineth 🇸🇪
Would Constructors in the same class can be linked with each other via the keyword base be correct?
Jimmacle
Jimmacle3w ago
"linked with" is odd phrasing they're already "linked" in the sense that Employee derives from Person base is a way to specifically access members of the base class
Merineth 🇸🇪
members?
Jimmacle
Jimmacle3w ago
properties, fields, methods
Merineth 🇸🇪
methods and attributes = members? aah fields? hahah
Jimmacle
Jimmacle3w ago
protected string ssn = "444-55-6666"; is a field
Merineth 🇸🇪
oh In other languages, i’m assuming these are called variables?
Jimmacle
Jimmacle3w ago
as for constructors specifically, the most accurate i could make your statement would be Constructors in the same class can call each other via the this keyword and can call constructors in the base class using the base keyword they can be, it's not quite correct i see "attributes" being a common name for them, but in C# that means something different
Merineth 🇸🇪
hmm
Jimmacle
Jimmacle3w ago
variables more refer to local variables inside a method or some executing piece of code
Merineth 🇸🇪
i was under the impression that classes had attributes and methods only?
Jimmacle
Jimmacle3w ago
technically correct
Merineth 🇸🇪
hence why UML class diagrams have them only
Jimmacle
Jimmacle3w ago
C# also has properties, which are syntax sugar for get/set methods
Merineth 🇸🇪
so what’s the difference between a field and a attribute? haven’t used them too much yet
Jimmacle
Jimmacle3w ago
in C# an attribute is completely different, it's basically extra information you can put on a class or member for example
class MyClass
{
[JsonProperty("my_property")] // <-- attribute
public string MyProperty { get; set; }
}
class MyClass
{
[JsonProperty("my_property")] // <-- attribute
public string MyProperty { get; set; }
}
other code can read these attributes and do stuff with them, like this one would tell a json serializer library what name to use for the property in the class's json representation
Merineth 🇸🇪
It was explained as defining a person . Where the attributes are information about a person such as age, name etc. e.g Int age = 2 and methods are stuff they can do or perform such as running
Jimmacle
Jimmacle3w ago
as for comparing to UML, a C# field is a UML attribute
Merineth 🇸🇪
i see That does clarify it a bit. I’ll return tomorrow, eepy time Thanks king 👑
Want results from more Discord servers?
Add your server