Hello, all. I'm trying to teach myself C# basics largely because I found its syntax cleaner in addition to it being used for an upcoming grad course.
I am trying to follow code in this book, which has been fine up until the preliminary to delgates. Specifically:
An abstract class is given for DocumentProcesses:
Code:
abstract class DocumentProcess
{
public abstract void Process(Document doc);
}
The processes take the form of:
Code:
class SpellcheckProcess : DocumentProcess
{
public override void Process(Document doc)
{
DocumentProcesses.Spellcheck(doc);
}
}
Document is a defined class with simple properties. Question 1: Why didn't they just use an interface if there's no "default" code needed and a must-have override?
The Document Processor has the code as follows:
Code:
class DocumentProcessor
{
private readonly List<DocumentProcess> processes =
new List<DocumentProcess>();
public List<DocumentProcess> Processes
{
get
{
return processes;
}
}
public void Process(Document doc)
{
foreach(DocumentProcess process in Processes)
{
process.Process(doc);
}
}
}
Question 2: Since the list is a readonly field and private, how can it be accessed via anything other than a constructor?
This block of code seems to do just that (the author does not mention it as a part of the DocumentProcessor class):
Code:
static DocumentProcessor = Configure()
{
DocumentProcessor rc = new DocumentProcessor
rc.Processes.Add(new TranslateIntoFrench); //etc...;
}
This seems to be accessing the Processes Property, which only has a get. Question 3: How is this accessing the private readonly List, especially to add processes?
And later:
Code:
DocumentProcessor processor = Configure();
-- in a Main method
Question 4: I understand that Configure() returns a DocumentProcessor object to this reference, but how does it do so? This confusion is related to questions 1-3
Thanks. I'd really like to move on to delegates, but I want to understand this first as opposed to memorizing it. I apologize for the indentation.
_CuriousOne