# Shortening my questions code

Is there any other way to shorten this code without sacrificing readability? I am fairly new to C#, let alone programming, and I wanted to know if this is as short as this code can possibly become. Feedback would be greatly welcomed.

Side note: was Questions.Questask used properly here, or could there have been a better way?

 class Program
{
static void Main(string[] args)
{
string name = "";
while (name.ToLower() != "none")
{
Console.WriteLine("Are you Kydd,Leo,Jay,Sha, or Zigg");

if (name.ToLower() == "kydd")
else if (name.ToLower() == "leo")
else if (name.ToLower() == "jay")
else if (name.ToLower() == "sha")
if (name.ToLower() == "zigg")
}
}

class Question
{
{
Console.WriteLine(question);
{
Console.WriteLine("You are right!");
}
else
{
System.Diagnostics.Process.Start(url);
}
}
}
}

• Stick the result of ToLower() into a new variable. You'll only have to call ToLower() once that way. Oct 26, 2013 at 2:48
• For any non-trivial application, the questions and names would be stored in some sort of data repository like an XML file or database, not emblazoned into the actual code itself. Oct 26, 2013 at 2:49
• You could place "Input Question Here" in a string instead of repeating it many times. Also, do you really want a Console.ReadKey() at the class code? Oct 26, 2013 at 2:55

I rewrote this for you, making it easier to understand and making it a little more efficient (although, the size is just a tad bigger then your original). Hopefully it makes a little more sense and you can ask any questions if you need to:

class Program
{
static void Main(string[] args)
{
var names = new List<NameClass>
{
new NameClass {Name = "Kydd", Answer = "Kquestion"},
new NameClass {Name = "Leo", Answer = "Lquestion"},
new NameClass {Name = "Jay", Answer = "Jquestion"},
new NameClass {Name = "Sha", Answer = "Squestion"},
new NameClass {Name = "Zigg", Answer = "Zquestion"}
};

string name = "";
while (name.ToLower() != "none")
{
Console.WriteLine("Are you Kydd,Leo,Jay,Sha, or Zigg");

var a = names.FirstOrDefault(z => z.Name.ToLower() == name);
if (a != null)
{
}
}
}

{
Console.WriteLine(question);
{
Console.WriteLine("You are right!");
}
else
{
System.Diagnostics.Process.Start(url);
}
}

public class NameClass
{
public string Name { get; set; }
public string Question { get; set; }
public string Answer { get; set; }
public string Url { get; set; }

public NameClass()
{
Question = "Input question here";
}
}
}


The beauty of this code is that you don't need a bunch of if statements. If you need to add more, simply add one more line of code in the names definition.

• It would be helpful if you explained what exactly did you change. Oct 26, 2013 at 11:07
• Thank you for this Beauty of a Code It will help me better understand the use of classes which I still struggle with. Just one Question What exactly does names.FirstOrDefault(z => z.Name.ToLower() == name) line of do kinda stumped on it Oct 26, 2013 at 15:31
• It's a LINQ expression that returns the first NameClass in the list names that has a Name.ToLower that equals name or a default value (null). Oct 26, 2013 at 23:33

When I need a simple strategy pattern I'll use a Dictionary. This can drastically reduce code by cutting repetition.

Note the use of StringComparer.InvariantCultureIgnoreCase for comparer to do away with the String.ToLower calls.

private static readonly IDictionary<string, Tuple<string, string>> NamesDictionary =
new Dictionary<string, Tuple<string, string>>(StringComparer.InvariantCultureIgnoreCase)
{
{ "kydd", Tuple.Create("question 1", "link1") },
{ "zydd", Tuple.Create("question 2", "link2") },
...
};

private static void DoSomething(string name)
{

return;


• +1 for being the only answer that renames Questask to AskQuestion :) Actually since the class is Question and the method is static, a perhaps even better name would be simply Ask()... Oct 26, 2013 at 23:09
• @retailcoder I didn't bother with it in my answer, but a more complex example I could understand the need for a Question class. OP's example would have been just fine without Question and instead have a static Program.AskQuestion. Oct 26, 2013 at 23:32
string[] names = {"kydd","leo","jay","sha","zigg");