Writeline with a Class

Console.Writeline in class method. Better Design

It's good to be thinking about things like this. It will definitely help you out with more complex projects. You could certainly create an IWritable interface or an abstract class with concrete implementations. Something like:

public interface IWritable
{
void Write(string text);
}

public class ConsoleWriter : IWritable
{
public void Write(string text)
{
Console.WriteLine(text);
}
}

And then your Drive() method can accept an IWritable parameter.

public class Car
{
public void Drive(IWritable writable)
{
writable.Write("I'm driving!");
}
}

new Car().Drive(new ConsoleWriter());

Of course, if your class has several methods that need to do something similar, you might consider changing the constructor to accept an IWritable parameter and storing it in a field:

public class Car
{
private readonly IWritable _writer;

public Car(IWritable writer)
{
_writer = writer;
}

public void Drive()
{
_writer.Write("I'm driving!");
}
}

Another benefit of doing things this way is it makes unit testing your class much simpler. You can pass it some innocuous writer that won't mess with your log file or update your database, but still let's you test the class.

How can I use an own class for Console.WriteLine override?

You can create a class that derives from TextWriter:

public class MyWriter : TextWriter
{
public override void Write(char value)
{
//Do something, like write to a file or something
}

public override void Write(string value)
{
//Do something, like write to a file or something
}

public override Encoding Encoding
{
get
{
return Encoding.ASCII;
}
}
}

and set the Console output to an instance of that class:

Console.SetOut(new MyWriter());

Output a List that contains a Class

Yeah that's right since calling Console.Writeline() directly like that calls the default ToString() method which does what you have observe. What you have to do is loop through the list and display each object in it like

foreach(var item in EmployeeList)
{
console.writeline(item.name +"\t"+item.address);
}

Moreover, your EmployeeList collection is of type Employee as seen below

list<Employee> EmployeeList = new list<Employee>();

Then the below code block is invalid unless Manager is of type Employee

Employee newEmployee = new Employee(name, address);
newEmployee = new Manager(name, address, salary, bonus);
EmployeeList.Add(newEmployee);

Why writing items to console writes only namespace and class name instead of data?

You are passing object to the Console.WriteLine(item) instead of passing the string. Console.WriteLine invokes ToString() method of that object that by default returns namespace+class name. You can override this behavior like next:

    public class City //class
{
public string CityName { get; set; }
public int Temperature { get; set; }

public City(string name, int temp)//konstruktor
{
this.CityName = name;
this.Temperature = temp;
}

public override string ToString()
{
return string.Format("{0} {1}", CityName, Temperature);
}

}

Or you can use another overload of WriteLine method:

Console.WriteLine("{0} {1}", item.CityName, item.Temperature);

Possible to output to console from within a class library C#?

Yup, Console.WriteLine etc will work just fine in a class library... but there's no guarantee that anything is listening to the console. If you use it from a WinForms app or a web app, that output may well go absolutely nowhere...

Have you thought of using a logging library such as log4net instead?



Related Topics



Leave a reply



Submit