by pietman
25. November 2009 13:14
This is a simple example of how to sort a Generic List List(T)
using System;
using System.Collections.Generic;
public class MySortExample
{
private static int CompareVehiclesByWeight(Vehicle x, Vehicle y)
{
if ((x == null) && (y == null))
return 0;
if ((x == null) && (y != null))
return -1;
if ((x != null) && (y == null))
return 1;
return (x.Weight.CompareTo(y.Weight));
}
public static void Main()
{
List<Vehicle> Vehicles = new List<Vehicle>();
Console.WriteLine("\nCapacity: {0}", Vehicles.Capacity);
Vehicles.Add(VehicleType.Car);
Vehicles.Add(VehicleType.Bicycle);
Vehicles.Add(VehicleType.Plane);
Vehicles.Add(VehicleType.Ship);
Vehicles.Add(VehicleType.Motorbike);
Vehicles.Sort(CompareVehiclesByWeight);
}
}
You can created multiple sorting algorithms, sorting on different elements/fields and then just call the sort method that you want to use at the appropriate time.
4ff30b66-3faa-4e10-a4c4-ae93139c69c5|0|.0
Tags:
c# | Generics
by pietman
25. November 2009 00:15
| Operator category |
Operators |
| Arithmetic |
+ - * / % |
| Logical (boolean and bitwise) |
& | ^ ! ~ && || true false |
| String concatenation |
+ |
| Increment, decrement |
++ -- |
| Shift |
<< >> |
| Relational |
== != < > <= >= |
| Assignment |
= += -= *= /= %= &= |= ^= <<= >>= |
| Member access |
. |
| Indexing |
[] |
| Cast |
() |
| Conditional |
?: |
| Delegate concatenation and removal |
+ - |
| Object creation |
new |
| Type information |
as is sizeof typeof |
| Overflow exception control |
checked unchecked |
| Indirection and Address |
* -> [] & |
a6028358-e293-44be-8272-fe3a75c1e662|0|.0
Tags:
c#
by pietman
25. November 2009 00:11
| Character |
Description |
Examples |
Output |
| C or c |
Currency |
Console.Write("{0:C}", 2.5);
Console.Write("{0:C}", -2.5);
|
$2.50
($2.50)
|
| D or d |
Decimal |
Console.Write("{0:D5}", 25); |
00025 |
| E or e |
Scientific |
Console.Write("{0:E}", 250000); |
2.500000E+005 |
| F or f |
Fixed-point |
Console.Write("{0:F2}", 25);
Console.Write("{0:F0}", 25);
|
25.00
25
|
| G or g |
General |
Console.Write("{0:G}", 2.5); |
2.5 |
| N or n |
Number |
Console.Write("{0:N}", 2500000); |
2,500,000.00 |
| X or x |
Hexadecimal |
Console.Write("{0:X}", 250);
Console.Write("{0:X}", 0xffff);
|
FA
FFFF
|
in a similar manner we can format dates:
//output: Apr 21, 2010
string s = DateTime.Now.ToString("MMM dd, yyyy");
//output: 2010/Apr/21
string s = DateTime.Now.ToString("yyyy/MMM/dd");
//output: 2010/04/21 .... 18/00/23/06
string s = DateTime.Now.ToString("yyyy/MM/dd .... HH/mm/ss/hh");
//output: 20100421
string s = string.Format("{0:yyyyMMdd}",DateTime.Now);
b644ca3e-42da-4a3e-b34b-ed109f2e9923|0|.0
Tags:
c#