If we remove the curly braces, Name, and Salary and we write like as follows list.Add(new Employee("Steve", 10000)); Program is requesting a public constructor. And we add a public constructor. The output is as follows. Please explain the reason. Modification is highlighted in the program.
/*
8000,Lucy
500000,Bill
10000,Andrew
10000,Janet
10000,Steve
*/
using System;
using System.Collections.Generic;
class Employee : IComparable
{
public int Salary { get; set; }
public string Name { get; set; }
public Employee(string Name, int Salary)
{
this.Name = Name;
this.Salary = Salary;
}
public int CompareTo(Employee other)
{
// Alphabetic sort if salary is equal. [A to Z]
if (this.Salary == other.Salary)
{
return this.Name.CompareTo(other.Name);
}
// Default to salary sort. [High to low]
return other.Name.CompareTo(this.Name);
}
public override string ToString()
{
// String representation.
return this.Salary.ToString() + "," + this.Name;
}
}
class Program
{
static void Main()
{
List
list.Add(new Employee("Steve", 10000));
list.Add(new Employee("Janet", 10000));
list.Add(new Employee("Andrew", 10000));
list.Add(new Employee("Bill", 500000));
list.Add(new Employee("Lucy", 8000));
// Uses IComparable.CompareTo()
list.Sort();
// Uses Employee.ToString
foreach (var element in list)
{
Console.WriteLine(element);
}
Console.ReadKey();
}
}
Posted Aug 23, 2012, 2:20 PM
VulpesPosted Aug 23, 2012, 11:06 AM
If you provide one, then it compiles and works fine:
Posted Aug 23, 2012, 10:33 AM
using System;
using System.Collections.Generic;
class Employee : IComparable
{
public int Salary;
public string Name;
public Employee(int Salary, string Name) //public constructor
{
this.Salary = Salary;
this.Name = Name;
}
public int CompareTo(Employee other)
{
// Alphabetic sort if salary is equal. [A to Z]
if (this.Salary == other.Salary)
{
return this.Name.CompareTo(other.Name);
}
// Default to salary sort. [High to low]
return other.Salary.CompareTo(this.Salary);
}
public override string ToString()
{
// String representation.
return this.Salary.ToString() + "," + this.Name;
}
}
class Program
{
static void Main()
{
List
list.Add(new Employee() { Name = "Steve", Salary = 10000 });
list.Add(new Employee() { Name = "Janet", Salary = 10000 });
list.Add(new Employee() { Name = "Andrew", Salary = 10000 });
list.Add(new Employee() { Name = "Bill", Salary = 500000 });
list.Add(new Employee() { Name = "Lucy", Salary = 8000 });
// Uses IComparable.CompareTo()
list.Sort();
// Uses Employee.ToString
foreach (var element in list)
{
Console.WriteLine(element);
}
Console.ReadKey();
}
}
/*
500000,Bill
10000,Andrew
10000,Janet
10000,Steve
8000,Lucy
*/
VulpesPosted Aug 23, 2012, 8:39 AM
If you're designing a class yourself, then you might decide to supply several constructors, including a parameterless one for users who like to use object initializers.
Posted Aug 23, 2012, 8:25 AM
VulpesPosted Aug 23, 2012, 8:06 AM
In contrast, in the program in your opening post to this thread, the Employee class does have a single public constructor which takes two parameters which initialize the Name and Salary fields. Consequently, it makes no sense to use an object initializer in this program.
Posted Aug 23, 2012, 7:45 AM
object initializer. We have to provide a public constructor the program to execute, otherwise program is not executing.
VulpesPosted Aug 23, 2012, 7:16 AM
If you recall, the default constructor takes no parameters and does nothing apart from calling the base class's parameterless constructor.
Posted Aug 23, 2012, 7:16 AM
VulpesPosted Aug 23, 2012, 6:53 AM
Posted Aug 23, 2012, 6:47 AM
Posted Aug 23, 2012, 6:19 AM
VulpesPosted Aug 23, 2012, 5:22 AM