C
C#8mo ago
JTN

✅ any improvements?

I have a small code and i want to know if i can improve anything
8 Replies
JTN
JTNOP8mo ago
C#
using System;

namespace IDK
{
public class Functions
{
public static string AskString(string Question)
{
while (true)
{
Console.WriteLine(Question);
string? UserInput = Console.ReadLine()!.ToLower();
if (!string.IsNullOrEmpty(UserInput) )
{return UserInput; }
}
}
public static bool AskYesOrNoQuestion(string Question)
{

var UserInput = AskQuestion(Question, ["y", "yes", "n", "no"]);

return new string[] { "y", "yes" }.Contains(UserInput);
}
public static string AskQuestion(string Question, string[] Awsners)
{
while (true)
{
Console.WriteLine(Question);
string? UserInput = Console.ReadLine()!.ToLower();
if (Awsners.Contains(UserInput))
{ return UserInput; }
}
}

}
}
C#
using System;

namespace IDK
{
public class Functions
{
public static string AskString(string Question)
{
while (true)
{
Console.WriteLine(Question);
string? UserInput = Console.ReadLine()!.ToLower();
if (!string.IsNullOrEmpty(UserInput) )
{return UserInput; }
}
}
public static bool AskYesOrNoQuestion(string Question)
{

var UserInput = AskQuestion(Question, ["y", "yes", "n", "no"]);

return new string[] { "y", "yes" }.Contains(UserInput);
}
public static string AskQuestion(string Question, string[] Awsners)
{
while (true)
{
Console.WriteLine(Question);
string? UserInput = Console.ReadLine()!.ToLower();
if (Awsners.Contains(UserInput))
{ return UserInput; }
}
}

}
}
Angius
Angius8mo ago
First, I recommend not silencing null-warnings with !. In your case you can use null-propagation instead, with ? Some style issues, too. Local variables and method parameters are camelCase not PascalCase, for example And
if (foo)
{ return 1; }
if (foo)
{ return 1; }
is not really following the style either. It would be either
if (foo)
{
return 1;
}
if (foo)
{
return 1;
}
or
if (foo) return 1;
if (foo) return 1;
Besides that, it looks fine
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega8mo ago
Should probably use file-scoped namespace, and the class can be static
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius8mo ago
No description
Angius
Angius8mo ago
Not a clone
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server