Basic stuff: c#
so i am new to programming i only learnt the basics, so please don't laugh at this, but i don't quite understand using classes in other classes(not program.cs i mean other classes).
for example:
internal class WritingTool
{
private string name;
private string metrial;
private double price;
private bool isUsed;
}
internal class PencilCase
{
.....
}
how can i use the WritingTool class and its properties in PencilCase and how it works. and i don't mean parent child classes.
4 Replies
i mean not as a whole for example creating arr of it
like private WritingTool[] writingTools;
but how do i use it
Can I ask you what you mean by "using" the class inside the other?
Because there are multiple use cases, and they have different outcomes.
If you just want to use the class as just an embedded class, you can do so:
Or if you just want to have an array of this
WritingTool
s in PencilCase
:
This would setup your WritingTool
array.classes in other classes (or types in other types in general) have the advantage that you can access the private members from the "outer" class.
a good example would be something like implementing the
IEnumerable<T>
interface
(i left out a lot of the actual stuff needed to implement the interface correctly, but its to showcase)
so here, instead of having to pass the _data
and _multiplier
directly to the Enumerator
's constructor, you can simply pass the reference to the collection's instance and still use its private members without having so much bloatyep