C
C#4mo ago
Ginger

✅ inheritance

I understand what inheritance is and how it works in a language like java, but im having some trouble with the syntax in c#, particularly with constructors and taking arguments in them and the keyword base, which i cant quite figure out how to use
46 Replies
Jimmacle
Jimmacle4mo ago
do you have an example of what you're trying to do that isn't working?
Ginger
GingerOP4mo ago
Yeah one second i gotta type it out
Jimmacle
Jimmacle4mo ago
a C# constructor that calls a base constructor would look something like
class MyClass : ParentClass
{
public MyClass(string arg1, int arg2) : base(arg2)
{
.. other code here
}
}

class ParentClass
{
public ParentClass(int arg1)
{
.. other code here
}
}
class MyClass : ParentClass
{
public MyClass(string arg1, int arg2) : base(arg2)
{
.. other code here
}
}

class ParentClass
{
public ParentClass(int arg1)
{
.. other code here
}
}
Ginger
GingerOP4mo ago
I have an animal class with 2 attributes: size and legs (legs is a paramater, size is set to 5) I want to create a chicken class that takes in a parameter for the legs, and had a size attribute of 3 What would the constructor for the chicken class look like My animal constructor is public Animal(int l) { legs = l; } Both attributes are protected
Jimmacle
Jimmacle4mo ago
since the attributes are protected you can set them both directly from the chicken constructor but it would be good practice to call the base constructor to set the legs
Ginger
GingerOP4mo ago
How would i do that
Jimmacle
Jimmacle4mo ago
the same way you would as if they were defined directly in the chicken class
Ginger
GingerOP4mo ago
My current constructor is public chicken() : base(legs) { size = 2; }
Jimmacle
Jimmacle4mo ago
yeah, something like that
Ginger
GingerOP4mo ago
But theres an error
Jimmacle
Jimmacle4mo ago
what's the error? oh, no legs your legs variable needs to come from somewhere
Ginger
GingerOP4mo ago
How would i do that
Jimmacle
Jimmacle4mo ago
public MyClass(string arg1, int arg2) : base(arg2)
Ginger
GingerOP4mo ago
It works Thank you
Jimmacle
Jimmacle4mo ago
:pepeok:
Ginger
GingerOP4mo ago
Actually
Jimmacle
Jimmacle4mo ago
$close
MODiX
MODiX4mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Ginger
GingerOP4mo ago
Oh is this closed Ok Nvm
Jimmacle
Jimmacle4mo ago
it's not
Ginger
GingerOP4mo ago
Oh
Jimmacle
Jimmacle4mo ago
that's just a message saying to close it if you're done, but if you're not it's fine :LUL:
Ginger
GingerOP4mo ago
Whats the shorthand for getters and setters?
Jimmacle
Jimmacle4mo ago
like the shortest way to define a property?
public string MyString { get; set; }
public string MyString { get; set; }
Ginger
GingerOP4mo ago
Whats the point in getters and setters there? Its public
Jimmacle
Jimmacle4mo ago
in that specific example it's a more technical reason, but just take my word for it that any public attributes should be properties and not fields
Ginger
GingerOP4mo ago
Whats the difference? Like whats a property and a field
Jimmacle
Jimmacle4mo ago
the actual compiled code is different, and it would be a binary breaking change to change an attribute from a field to a property or vice versa properties are secretly methods that work on a hidden field
Ginger
GingerOP4mo ago
Right
Jimmacle
Jimmacle4mo ago
it's basically a shorter way of using an attribute with getAttribute and setAttribute methods in java
Ginger
GingerOP4mo ago
I see But why would you need getters and setters for a public variable here? Or does that work with private and protected too
Jimmacle
Jimmacle4mo ago
the visibility of the property and the visibility of each get/set can be different for example, make it private set; so only the class can change it but anything can read it
Ginger
GingerOP4mo ago
So i can do private var a { public get; public set; } ?
Jimmacle
Jimmacle4mo ago
no, that wouldn't make sense the getters and setters can't be more visible than the overall property, but they can be less visible
Ginger
GingerOP4mo ago
So how do i make a private attribute that has getters and setters i can use globally
Jimmacle
Jimmacle4mo ago
that wouldn't be private at all
Ginger
GingerOP4mo ago
Ah
Jimmacle
Jimmacle4mo ago
if you want to access it outside the class you'd want it to be public
Ginger
GingerOP4mo ago
So i should just use public attributes and ditch the getters and setters?
Jimmacle
Jimmacle4mo ago
no, you should do what i showed earlier
public string MyString { get; set; }
public string MyString { get; set; }
if it's public, it should be a property (having get and/or set)
Ginger
GingerOP4mo ago
But why not public string MyString; Then i can access it with class.MyString
Jimmacle
Jimmacle4mo ago
you can do the same with this, but like i said there are technical reasons this is better practice basically it's easier to change in the future without breaking any code that may depend on it
Ginger
GingerOP4mo ago
So this works but your way is better practice?
Jimmacle
Jimmacle4mo ago
right
Ginger
GingerOP4mo ago
I see thanks for the all the help man
Jimmacle
Jimmacle4mo ago
:pepeok:
Want results from more Discord servers?
Add your server