http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/867f655285c0e04d/8a59fb7d2904c775?tvc=1&q=saar+decimal#8a59fb7d2904c775Could anyone help me to understand why in the following code sum1 is different from sum2?
Basically it's the same calculation done. Keep in mind the x is 28 digits so it's within the range of decimal.
decimal x = (-0.0084682975822291150192357277M);
decimal sum1 , sum2;
sum2 = (3500.91M * x) + (x * 3500M + 822m);
sum1 = (3500.91M * x) + (x * 3500M) + 822m;
Console.WriteLine (sum1);
Console.WriteLine (sum2);
------------
Well, the reason is that the decimal type precision s not 28 right to the decimal digit, but 28 digits overall. Thanks for those who answered.
Jon Skeet posted an example of Singletons and inheritance.
What do you think about this generic singleton solution? This way you dont have to update the factory method with each subclass creation. It would be neater if the new generics' constraint would support parameters, but you can overcome it with abstract method CreateInstance which returns T.
abstract class DataProvider<T> where T: new()
{
private static T mInstance = new T();
public T Instance
{
get
{
return mInstance;
}
}
public abstract void Connect();
}
class Oracle : DataProvider<Oracle>
{
public override void Connect()
{
Console.WriteLine ("Connecting to Oracle");
}
}
class SqlServer : DataProvider<Oracle>
{
public override void Connect()
{
Console.WriteLine ("Connecting to Sql Server");
}
}