C
C#2y ago
yatta

How to write function in .net6? [Answered]

I try .net6 for the first time and i got an error ask me to provide return value regardless that i already provided one.
8 Replies
MechWarrior99
MechWarrior992y ago
That only returns if the condition in the if statement is true. You need a return regardless of conditions.
yatta
yatta2y ago
like this ? but error still there
MechWarrior99
MechWarrior992y ago
No, because now the for loop will only run ones.
static int Search(int[] nums, int target)
{
for(int i = 0; i < nums.Length; i++)
{
if (nums[i] == target)
return i;
}

return -1;
}
static int Search(int[] nums, int target)
{
for(int i = 0; i < nums.Length; i++)
{
if (nums[i] == target)
return i;
}

return -1;
}
Tvde1
Tvde12y ago
there is no guarantee that the code will enter the for loop
MechWarrior99
MechWarrior992y ago
You need to add a return at the very end where it is not based on any conditions or anything. Sort of like fallback if everything else fails/doesn't run at least that will return.
yatta
yatta2y ago
sound like something in C
Tvde1
Tvde12y ago
also, .IndexOf exists :)
[1, 5, 7, 3, 12, 54].IndexOf(7); => 2
[1, 5, 7, 3, 12, 54].IndexOf(7); => 2
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts
TimeSpan formatter [Answered]Hi, is there any function or library that would take an time length input, for example `5 min` or `3How do you pass into a method a generic after you've reflected? [Answered]Suppose I suspect that this IEnumerable (non-generic) is also an IReadOnlyList<T> where T : struct. Signalr not sending message(s) to specific user(s)in the hub i have setup this signature for a method ```cs public async Task SendMessage(string sendRider line problemI'd moved to rider but a line make me angry, anybody can help me about how can I remove this?Is it possible to publish a WPF Application into a single file with the font inside of the EXE?Is it possible to publish a WPF Application into a single file with the font inside of the EXE?Binding to Dictionary in WPF with a binding KEYhello i have a very specific problem related to WPF : i have a listbox with itemsSource <ListBox Itset default value of parameter to other parameter [Answered]ok, i am aware that this is probably not possible, but can i set default value of parameter x to vaChecking if an arbitrary (remote) URL is a file attachment, and extracting it if it is? [Answered]If I'm given an arbitrary URL (I don't have control over it), how can I check if it appears to be a How does assigning default values when creating an instance work? [Answered]I'm not sure how to ask this question. I also can't for the life of me remember what this style of iProcess.EnableRaisingEvents does not work on WindowsI have an app that have 20 workers that are other processes. On production, on ubuntu, when I close