TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Maha
NA
0
327.8k
Object initializer
Oct 28 2013 6:10 PM
Original program is given in the following website and modified incomplete program is given below. Please help me to execuate this program. Modification is coloured.
http://www.dotnetperls.com/icomparable
using System;
using System.Collections.Generic;
class Employee : IComparable<Employee>
{
private
int salary;
private
string name;
public
Employee(string name, int salary)//public constructor
{
this.salary = salary;
this.name = name;
}
public
string getName()
{
return name;
}
public
int getSalary()
{
return 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.salary.CompareTo(this.salary);
}
public override string ToString()
{
// String representation.
return this.salary.ToString() + "," + this.name;
}
}
class Program
{
static void Main()
{
List<Employee> list = new List<Employee>();
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
*/
Reply
Answers (
7
)
C #.net and data base
C#