Hey folks.
I’m in the early stages of learning C# and I’ve run into an issue that I can’t solve to save my life. It’s probably super obvious and I’m hoping someone with more experience can help me out.
Why does this code result in Error CS0161 ‘Program.Discount(int)’ Not all code paths return a value?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int totalPrice = Convert.ToInt32(Console.ReadLine());
Discount(totalPrice);
}
static int Discount(int x)
{
if (x >= 10000)
{
Console.WriteLine(x – (0.2 * x));
}
else
{
Console.WriteLine(x);
}
}
}
}
Your discount method isn’t returning anything, but the method signature says it should return an int.
You should probably change that to void so it knows there will be no return from that method.
Wow. that literally fixed it instantly lmao thank you so much!
You declared an int
as the return type of your static int Discount(int x)
method. If you don’t want to return a value, use void
.
Otherwise the compiler will complain, that some or all of the possible code paths do not return a value.
that was it! fixed my problem right away. thanks!
C# devs
null reference exceptions