Your code:
public static User FindUser(User[] users, int noteid)
{
foreach (User user in users)
if (user.ID == noteid) return user.FullName;
return null;
}
Is expecting to return a string object (the user.FullName value) when it succeeds in finding one, whereas the return type you've declared for your method is User. What you should do is this (where you're currently calling FindUser:
var ArrayOfUsersToSearch = GetAnArrayOfUserFromSomewhere();
var noteId = 3; // Or whatever value you want to use
var myUserToFind = FindUser(ArrayOfUsersToSearch, noteId);
Console.WriteLine("The users full name is: {0}",
myUserToFind != null ? myUserToFind.FullName : "Not Found");
And change the code in your FindUser method to be:
public static User FindUser(User[] users, int noteid)
{
foreach (User user in users)
if (user.ID == noteid) return user;
return null;
}
The reason you see "UserCustomerNotes.User" output when you return user from your method is this:
Console.WriteLine(FindUser(users, noteId));
// instead of
Console.WriteLine(FinderUser(users, noteId).FullName);
Because the return type is UserCustomerNotes.User and it doesn't have an override of "ToString" to specify anything different, the default value that is given when Console.WriteLine asks your User object for its textual equivalent is its name. This explains why you see the "odd" output from your code currently.
Note: I've put Console.WriteLine("The users full name is: {0}", myUserToFind != null ? myUserToFind.FullName : "Not Found"); as your code can return null if it fails to find a user and you'd want to special-case the output you provide in that circumstance.
It's also worth mentioning, but unrelated to the question itself, that the format:
foreach(var x in y)
do something;
Can be more prone to "breaking" if changes are made further down the line than if you explicitly call out the { and } around each statement block:
foreach (User user in users)
{
if (user.ID == noteid)
{
return user.FullName;
}
}
return null;