0

I am use to using the decimal data type like this:

decimal Cost = 30M; 

This initialises the decimal with £30 (£ is in the context of my application). I am planning to use a type alias for Decimal in my application like this:

using Cost = System.Decimal 

I believe I have to use the object rather than the primitive type when using a type alias. Please let me know if that is not correct?

Also I have noticed that I cannot do this:

Decimal cost = new Decimal(30M); 

I have to do this instead:

Decimal cost = new Decimal(30); 

Is the initialisation code above suitable for a currency?

2
  • 2
    new Decimal? Why would you want to use a Decimal constructor? Also, why alias a built-in type? Commented Feb 3, 2018 at 15:40
  • @CamiloTerevinto I'm guessing to make explicit in code that the type is used as a monetary value. Commented Feb 3, 2018 at 15:48

2 Answers 2

1

This initialises the decimal with £30 (£ is in the context of my application). I am planning to use a type alias for Decimal in my application like this: using Cost = System.Decimal

Ok, thats a valid option.

I believe I have to use the object rather than the primitive type when using a type alias. Please let me know if that is not correct?

I'm not sure what you are asking, but if you are using a type alias, the whole idea is to use it:

var cost = new Cost(10); 

Rememnber, a type alias is a type alias; wherever you can System.Decimal, Cost is valid too.

Also I have noticed that I cannot do this: Decimal cost = new Decimal(30M);

Yup, and you can't do Cost cost = new Cost(30M); either, because decimal doesn't define such constructor overload. The reason being, if you already have a Decimal / Cost in hand why in the world would you want to new it up again? Cost cost = 30M; would do just fine.

Also notice that Cost cost = 30; is just fine too, the compiler will perform the implicit conversion for you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I also have a DateTime. Can I initialise this without the new operator?
@w0051977 No, C# does not support date constant literals (var d = #01/10/18 or similar), you need the new operator.
1
  1. Yes, you need to use the fully qualified type name to use an alias. From the docs:

    Create a using alias to make it easier to qualify an identifier to a namespace or type. The right side of a using alias directive must always be a fully-qualified type regardless of the using directives that come before it.

    It's worth noting that using a type alias is very uncommon, particularly when using a built in value type like decimal. I would not recommend that you do that.

  2. You can't do new Decimal(30M) because the type doesn't have a constructor that takes a decimal, after all it would be pointless because you can simply do this (where the M qualifier is optional):

    Decimal cost = 30M; 

3 Comments

The M qualifier is NOT optional. Without it, the number is treated as a double and generates a compiler error.
@DavidG: The following decimal dd = 30.54; produces compiler error CS0664 Literal of type double cannot be implicitly converted to type 'decimal'; use an 'M' suffix to create a literal of this type.
@MarkBenningfield Indeed, but this question doesn't contain that number, it contains an integer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.