Explain why the instance empty of the empty of the EmptyClass class has the ToString () method?
using System;
using System.Collection.Generic;
using System.Ling;
using System.Text;
namespace FinalExam2010
{
Class EmptyClass
{
}
class Program
{
static void Main(string [] args)
{
EmptyClass empty= new EmptyClass();
string s = empty.ToString();
}
}
}
Regrads
Johnny
Johnny NguyenPosted Apr 18, 2011, 9:20 PM
Mamta MPosted Apr 18, 2011, 1:26 AM
In C#, all classes implicitly derive from the Object class which is like the root class for all classes. This Object class defines a ToString() method to convert any object into a string form. Since as mentioned earlier, all classes derive from Object, this ToString() method can be used by any class. In your code, you are converting the object empty to a string form using this method. Here, usage of this method is not really useful. It would be useful in instances where say you want to a integer object to a string format or a date object to a string format and so on.
Hope this helps.
-Mamta