Einzelnen Beitrag anzeigen

Elvis

Registriert seit: 25. Nov 2005
Ort: München
1.909 Beiträge
 
Delphi 2010 Professional
 
#6

Re: Funktion verkürzen

  Alt 1. Mär 2006, 20:35
Auweia... ;?

Suche mal nach Boxing/Unboxing oder einfach ".Net ist langsam". Diese Aussage kommt oft in Zusammenhang mit dem CLR Feature Boxing. Dieses Parsing eines strings in einen int, der mal ein int war und doch zu einem String gewandelt wurde....

Generics sind hier ein Weg, damit du jeden Value type ohne Umwandlung gegen sein default[1] vergleichen kannst.
Einen int aus Faulheit in ein object zu boxen ist nur unter .Net 1.1 zu verzeihen.

[1]alle numerischen Value types haben 0 als default, bool hat false als default

Hier mal ein Bleistift wie man a) alle Value types gegen 0/default in einen bool wandeln kann.
Außerdem enthält es noch eine Methoe, die lle mit String vergleichbaren typen gegen 1, True oder true vergleicht.

Einfach als abstraktes Beispiel auffassen, das zeigen soll, wie man type contraint von generics dazu benutzen kann, um mit den generischen Werten überhaupt etwas anfangen zu können.
Code:
class Program
{
   static bool PrimitiveToBool<T>(T value)
      where T : struct, IEquatable<T>
   {
      return !default(T).Equals(value);
   }

   static bool StringToBool<T>(T value)
      where T : IEquatable<String>
   {
      return !object.Equals(value, default(T)) &&
             (value.Equals("1") ||
              value.Equals("True") ||
              value.Equals("true"));
   }


   public static void Main(string[] args)
   {
      TestPrimitive<Int32>(0);
      TestPrimitive<Int32>(1);
      TestPrimitive<Int64>(0);
      TestPrimitive<Int64>(1);
      TestPrimitive<Decimal>(0);
      TestPrimitive<Decimal>(1);
      TestPrimitive<Single>(0);
      TestPrimitive<Single>(1);

      TestString<String>("true");
      TestString<String>("mööp");

      TestString<StringCompatible>("1");
      TestString<StringCompatible>(new StringCompatible("hallo"));
   }

   const string format = "{0}: {1} is {2}";

   private static void TestPrimitive<T>(T value)
      where T : struct, IEquatable<T>
   {
      Console.WriteLine(format, typeof(T), value, PrimitiveToBool<T>(value));
   }

   private static void TestString<T>(T value)
      where T : IEquatable<String>
   {
      Console.WriteLine(format, typeof(T), value, StringToBool<T>(value));
   }
}

struct StringCompatible : IEquatable<String>
{
   String value;

   public bool Equals(String other)
   {
      return String.Equals(value, other);
   }

   public StringCompatible(String value)
   {
      this.value = value;
   }

   public override String ToString()
   {
      return value;
   }

   public static implicit operator StringCompatible(String value)
   {
      return new StringCompatible(value);
   }
}
edit:
Output
System.Int32: 0 is False
System.Int32: 1 is True
System.Int64: 0 is False
System.Int64: 1 is True
System.Decimal: 0 is False
System.Decimal: 1 is True
System.Single: 0 is False
System.Single: 1 is True
System.String: true is True
System.String: mööp is False
Samples.GenericToBool.StringCompatible: 1 is True
Samples.GenericToBool.StringCompatible: hallo is False
Robert Giesecke
I’m a great believer in “Occam’s Razor,” the principle which says:
“If you say something complicated, I’ll slit your throat.”
  Mit Zitat antworten Zitat