In this program we are using IComparable interface but we didn't declare interface as such
public interface IComparable
{
int CompareTo(object o);
}
I assume that once we implemented the interface we didn't need declaration that is way declaration is not there in this program. This is true for any programs. I wish to know whether my understanding is correct. Problem is highlighted.
using System;
public class SchoolCompare
{
public static void Main()
{
School[] school = new School[8];
for (int x = 0; x < school.Length; ++x)
{
school[x] = new School();
Console.Write("Name of School #{0} ", x + 1);
school[x].SchoolName = Console.ReadLine();
Console.Write("Students Enrollment Nnuber: ");
school[x].StudentEnrolled = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
}
Array.Sort(school);
for (int x = 0; x < school.Length; ++x)
Console.WriteLine("School #{0} , Name: {1}, Enrollement Number: {2}",
x + 1, school[x].SchoolName, school[x].StudentEnrolled);
}
}
class School : IComparable
{
private string sName;
private int enrolled;
public string SchoolName
{
get { return sName; }
set { sName = value; }
}
public int StudentEnrolled
{
get { return enrolled; }
set { enrolled = value; }
}
public int CompareTo(object o)
{
int returnVal;
School temp = (School)o;
if (this.enrolled > temp.enrolled)
returnVal = 1;
else
if (this.enrolled < temp.enrolled)
returnVal = -1;
else
returnVal = 0;
return returnVal;
}
}
Loading
VulpesPosted May 9, 2014, 12:01 PM
This .dll contains the fundamental types which any .NET program needs to function and is pre-compiled i.e. it contains 'intermediate language' (or IL) and metadata for those types, not the source code itself.
As you'll see from the MSDN docs, the System.IComparable interface is one of the fundamental types included in mscorlib.dll:
http://msdn.microsoft.com/en-gb/library/system.icomparable.aspx
So the compiler is able to check from the metadata that this interface exists and is being used properly. You're therefore correct that we don't need to redefine the interface in our programs because it's already been defined in mscorlib.dll.
Posted May 9, 2014, 4:50 PM
VulpesPosted May 9, 2014, 4:46 PM
It's declaration therefore needs to be present for the program to compile.
Posted May 9, 2014, 4:28 PM
//public interface IWorking
//{
// string Work();
//}
using System;
public interface IWorking
{
string Work();
}
class Employee : IWorking
{
private string name;
public Employee(string name)
{
this.name = name;
}
public string GetName()
{
return name;
}
public string Work()
{
return "I do my job";
}
}
abstract class Animal : IWorking
{
protected string name;
public Animal(string name)
{
this.name = name;
}
public string GetName()
{
return name;
}
public abstract string Work();
}
class Dog : Animal
{
public Dog(string name)
: base(name)
{
}
public override string Work() //override keyword is used because inherited from abstract class
{
return "I watch the house";
}
}
class Cat : Animal
{
public Cat(string name)
: base(name)
{
}
public override string Work()
{
return "I catch mice";
}
}
//p268
class DemoWorking
{
public static void Main()
{
Employee bob = new Employee("Bob");
Dog spot = new Dog("Spot");
Cat puff = new Cat("Puff");
Console.WriteLine("{0} says {1}", bob.GetName(), bob.Work());
Console.WriteLine("{0} says {1}", spot.GetName(), spot.Work());
Console.WriteLine("{0} says {1}", puff.GetName(), puff.Work());
Console.ReadKey();
}
}
/*
Bob says I do my job
Spot says I watch the house
Puff says I catch mice
*/