What is C - potatoscript/csharp GitHub Wiki

๐Ÿง‘โ€๐Ÿ’ป What is C#? ๐Ÿง‘โ€๐Ÿ’ป

C# (pronounced C-sharp) is a programming language used to write instructions that a computer can understand and follow. Think of C# as a way for you to talk to a computer, giving it steps to perform tasks. Just like how you might give a friend instructions to build a LEGO set, in C#, we give the computer a set of instructions to follow to complete a task.


๐Ÿ› ๏ธ Why do we need C#? ๐Ÿ› ๏ธ

Imagine you're in a huge library ๐Ÿ“š, and you want to find your favorite book. You could walk around looking for hours, or you could ask a librarian to help you. In this case, the librarian is like a computer. The library is like the information stored on a computer, and C# is the language that helps us communicate with the librarian (the computer) to quickly find the book (or complete a task).


๐ŸŒŸ The Basics of C#: A Story of a Little Robot ๐ŸŒŸ

Meet Cody, the little robot ๐Ÿค– who loves to help people by following clear instructions. But thereโ€™s one problem: Cody doesnโ€™t understand human language. He only understands C#, the language that the humans use to tell him what to do!

Let's see how we can help Cody complete a task.


๐ŸŽฌ Chapter 1: Cody's First Task ๐ŸŽฌ

You give Cody the task of saying "Hello, World!" to everyone in the room. This is how we can do it in C#:

using System;  // This line tells the robot where to look for certain tools

class Program   // The "Program" is where Cody's instructions live
{
    static void Main()  // Main is the starting point of Cody's task
    {
        Console.WriteLine("Hello, World!");  // Cody says hello
    }
}

๐Ÿ“œ Explanation of Codyโ€™s First Task ๐Ÿ“œ

  • using System;: This line tells Cody to use tools from the "System" toolbox, which has things like how to print text to the screen. Think of it like a robot asking for a screwdriver to fix something!
  • class Program: This is the "box" where Cody's instructions are kept. It's like putting all of Cody's task lists into one place.
  • static void Main(): This is the starting point where Cody begins working. It's like saying, "Cody, start here!"
  • Console.WriteLine("Hello, World!");: This line is where Cody actually follows the instruction to say "Hello, World!" Itโ€™s like telling him to speak aloud.

๐Ÿงฉ Chapter 2: Variables โ€“ Cody's Special Containers ๐Ÿงฉ

Cody doesnโ€™t just say things; he also likes to store information, like numbers and words. To help Cody keep track of things, we give him containers (called variables in C#) to hold things like numbers, text, or even true/false answers.

For example, let's say Cody has a special container to store your age:

using System;

class Program
{
    static void Main()
    {
        int age = 10;  // Cody has a container called 'age' where he keeps the number 10
        Console.WriteLine("I am " + age + " years old!");  // Cody says your age
    }
}

๐Ÿง  Explanation of Variables ๐Ÿง 

  • int age = 10;: This is a container named age that holds the number 10. We call this a variable because the value it holds can change.
  • Console.WriteLine("I am " + age + " years old!");: This is Cody showing the value of age. It tells him to print out something like, "I am 10 years old!"

๐ŸŽฎ Chapter 3: Decisions โ€“ Cody's Choices ๐ŸŽฎ

Cody loves making decisions! Sometimes he has to choose between two or more options. In C#, we can tell Cody to make decisions using if-else statements.

Imagine Cody is asked whether he is older than 5 years old. Based on his decision, he will answer:

using System;

class Program
{
    static void Main()
    {
        int age = 10;  // Cody's age is 10

        if (age > 5)   // If Cody is older than 5
        {
            Console.WriteLine("Cody is older than 5!");  // Cody says "Yes!"
        }
        else
        {
            Console.WriteLine("Cody is 5 or younger!");  // Cody says "No!"
        }
    }
}

๐Ÿ” Explanation of Decisions ๐Ÿ”

  • if (age > 5): This is the decision point. Cody checks if the value of age is greater than 5.
  • Console.WriteLine("Cody is older than 5!");: If the condition is true (yes, Cody is older than 5), this message is shown.
  • else: If the condition is false (if Cody isn't older than 5), then Cody says something else.

๐Ÿš€ Chapter 4: Cody's Repeat Tasks โ€“ Loops ๐Ÿš€

Sometimes Cody needs to repeat a task many times. Instead of telling him the same thing over and over, we can use loops to make Cody do things repeatedly!

For example, if Cody wants to say "Hello!" five times, we can use a for loop:

using System;

class Program
{
    static void Main()
    {
        for (int i = 0; i < 5; i++)  // Repeat 5 times
        {
            Console.WriteLine("Hello!");  // Cody says "Hello!"
        }
    }
}

๐Ÿ”„ Explanation of Loops ๐Ÿ”„

  • for (int i = 0; i < 5; i++): This tells Cody to start at 0 and repeat the task 5 times.
  • Console.WriteLine("Hello!");: Cody says "Hello!" each time.

๐Ÿง‘โ€๐Ÿ’ป Chapter 5: Cody's Big Project โ€“ Combining Everything ๐Ÿง‘โ€๐Ÿ’ป

Now that Cody knows how to store information, make decisions, and repeat tasks, letโ€™s give him a big project: organizing a party ๐ŸŽ‰ for his friends! Cody will check if it's a weekday, and if it is, he will invite his friends to the party!

using System;

class Program
{
    static void Main()
    {
        string day = "Friday";  // Cody checks what day it is

        if (day == "Friday")  // If it's Friday
        {
            Console.WriteLine("It's party time! ๐ŸŽ‰");  // Cody gets excited for the party!
        }
        else
        {
            Console.WriteLine("No party today, it's " + day + " ๐Ÿ˜ž");  // Cody says no party
        }
    }
}

๐ŸŽ‰ Explanation of Cody's Party ๐ŸŽ‰

  • string day = "Friday";: Cody stores the day of the week in a container called day.
  • if (day == "Friday"): Cody checks if today is Friday.
  • Console.WriteLine("It's party time! ๐ŸŽ‰");: If itโ€™s Friday, Cody gets excited and announces the party!
  • else: If it's not Friday, Cody says thereโ€™s no party.

๐ŸŽจ What C# Can Do! ๐ŸŽจ

C# can be used to build all sorts of amazing things:

  • Video games ๐ŸŽฎ
  • Mobile apps ๐Ÿ“ฑ
  • Websites ๐ŸŒ
  • Robots ๐Ÿค–
  • Simulations ๐Ÿ’ก