C#

Creating a Simple Quiz App in C#


Developing a quiz application in C# involves managing questions, user responses, and evaluating answers. In this example, we'll introduce the concept with a basic code snippet for a multiple-choice quiz.

Sample C# Code for Quiz App

Here's a basic example of C# code for a multiple-choice quiz:

using System;
using System.Collections.Generic;
class QuizQuestion
{
    public string Question { get; set; }
    public List<string> Options { get; set; }
    public int CorrectOptionIndex { get; set; }
    public bool IsCorrect(int selectedOption)
    {
        return selectedOption == CorrectOptionIndex;
    }
}
class QuizApp
{
    private List<QuizQuestion> questions = new List<QuizQuestion>();
    private int currentQuestionIndex = 0;
    private int score = 0;
    public void AddQuestion(QuizQuestion question)
    {
        questions.Add(question);
    }
    public QuizQuestion GetCurrentQuestion()
    {
        if (currentQuestionIndex < questions.Count)
        {
            return questions[currentQuestionIndex];
        }
        return null;
    }
    public bool NextQuestion()
    {
        currentQuestionIndex++;
        return currentQuestionIndex < questions.Count;
    }
    public int GetScore()
    {
        return score;
    }
    public void EvaluateAnswer(int selectedOption)
    {
        QuizQuestion currentQuestion = GetCurrentQuestion();
        if (currentQuestion != null && currentQuestion.IsCorrect(selectedOption))
        {
            score++;
        }
    }
}
class Program
{
    static void Main()
    {
        var quizApp = new QuizApp();
        var question1 = new QuizQuestion
        {
            Question = `What is the capital of France?`,
            Options = new List<string> { `Berlin`, `Madrid`, `Paris`, `Rome` },
            CorrectOptionIndex = 2
        };
        var question2 = new QuizQuestion
        {
            Question = `Which planet is known as the Red Planet?`,
            Options = new List<string> { `Earth`, `Jupiter`, `Mars`, `Venus` },
            CorrectOptionIndex = 2
        };
        quizApp.AddQuestion(question1);
        quizApp.AddQuestion(question2);
        while (quizApp.NextQuestion())
        {
            Console.WriteLine(`Question: ` + quizApp.GetCurrentQuestion().Question);
            int optionIndex = 0;
            foreach (string option in quizApp.GetCurrentQuestion().Options)
            {
                Console.WriteLine($`{optionIndex}. {option}`);
                optionIndex++;
            }
            int selectedOption;
            do
            {
                Console.Write(`Your answer (enter option number): `);
            } while (!int.TryParse(Console.ReadLine(), out selectedOption) || selectedOption < 0 || selectedOption >= quizApp.GetCurrentQuestion().Options.Count);
            quizApp.EvaluateAnswer(selectedOption);
        }
        Console.WriteLine(`Quiz completed. Your score: ` + quizApp.GetScore());
    }
}

This code defines a `QuizApp` class that allows you to add questions, evaluate user responses, and calculate the score. In a complete quiz app, you would create a user interface, save questions to a file or database, and potentially implement more quiz features.

Sample HTML Quiz Interface

Below is a simple HTML form for displaying and answering quiz questions:

    Written by Surfside Media

    Senior Full Stack Developer specializing in Web Technologies.