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 namedage
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 ofage
. 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 ofage
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 calledday
.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 ๐ก