can someone explain? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

can someone explain?

Hello, that's the code I have a problem with. The problem is, that it says I can't convert a double in decimal but the number is a decimal so what should I do? class Program { public delegate decimal Rechenoperation (decimal x, decimal y); static void Main(string[] args) { Rechenoperation plus = (x, y) => { return x + y; }; decimal result = decimal.Parse(plus(3.534M, 3)); Console.WriteLine(result); I also already tried this:: decimal result = decimal.Parse(plus(decimal.Parse(3.534M), 3)); and if I write it like this it says I can't convert from decimal to a string, but which string there are only numbers:: Rechaenoperation plus = (x, y) => { return x + y; }; decimal num1 = 3.534M; decimal result = decimal.Parse(plus(num1, 3)); Console.WriteLine(result); Please explain what is going on with c# or what I don't understand (what my mistake is).

6th Mar 2020, 6:21 AM
Michael
4 Answers
+ 1
Hello Michael This is just a speculation I guess, but it makes a bit sense. As I checked the reference page: https://docs.microsoft.com/en-us/dotnet/api/system.decimal.parse It says "Converts the string representation of a number to its Decimal equivalent." But in your code, `Rechenoperation` is a delegate which returns a `decimal`. So, practically, you were supplying a `decimal` into `decimal.Parse` method, which was unexpected, as the `decimal.Parse` was expecting a string as first argument. P.S. I don't know C# much, so I'm not too sure about that 'M' suffix in `decimal` number. Maybe you can tell me about that? Hth, cmiiw
6th Mar 2020, 9:04 AM
Ipang
+ 1
hello Ipang, the M after the decimal is a formal thing Like if you wtite a float you also do it like this float x = 3.14F; at least it is a formal thing in other languages like c++ in which you can write: float x = 1.31; or float x = 1.31F; its like you write classes BIG and if you have an interface you write an I bevore the name of the interface, so that if someone is going to read your code he instantly knows that it is a Decimal or a float. what i know is, that if you write: float x = 1.31; in c# it's an mistake, but in c++ not; at least that is how I understand it if something about this commend is false please commend it
6th Mar 2020, 11:56 AM
wcqkfnlkmfl
0
wcqkfnlkmfl Thanks for information buddy 🙏
7th Mar 2020, 4:11 AM
Ipang
0
Michael I suggest a removal of the words in tags Except for C#. The words 'help', 'lol', 'dont', 'understand' 'again' doesn't help improve context of question. But C# is fine 👌
7th Mar 2020, 9:51 AM
Ipang