No history yet

C# Basics

C# Building Blocks

At its core, a computer program tells a computer what to do, step by step. To give these instructions, we need a way to store and manage information. In C#, we use variables for this. Think of a variable as a labeled box where you can keep a piece of information.

Every box is designed to hold a specific type of information. You wouldn't store soup in a shoebox. Similarly, C# has different data types for different kinds of data. Here are a few common ones:

  • string: For text, like a video title. Always wrapped in double quotes.
  • int: For whole numbers, like the number of likes.
  • double: For numbers with decimal points, like a video's duration in minutes.
  • bool: For true or false values, like whether a video is public.
// Declaring and initializing variables
string videoTitle = "Understanding C# Basics";
int viewCount = 1024;
double videoLength = 15.5; // in minutes
bool isPublished = true;

Once you've stored information in a variable, you can use its label to access and modify it throughout your program.

Making Decisions and Repeating Actions

Programs rarely run in a straight line. They need to react to different situations. C# uses control structures to manage this flow. The most common one for decision-making is the if-else statement. It lets your code do one thing if a condition is true, and something else if it's false.

int comments = 5;

if (comments > 0)
{
  Console.WriteLine("This video has comments!");
}
else
{
  Console.WriteLine("Be the first to comment.");
}

Sometimes you need to perform the same action over and over. That's where loops come in. A for loop is great when you know exactly how many times you want to repeat an action.

// This loop runs 5 times
for (int i = 0; i < 5; i++)
{
  Console.WriteLine("Checking for new videos...");
}

A while loop is useful when you want to repeat an action as long as a certain condition remains true. It keeps going until the condition becomes false.

int newVideos = 3;

while (newVideos > 0)
{
  Console.WriteLine("Downloading a new video.");
  newVideos--; // Decrease the count by one
}

Thinking in Objects

C# is an object-oriented programming (OOP) language. This might sound complex, but the idea is simple: we can group related data and behaviors into neat little packages called objects. To create an object, you first need a blueprint, which in C# is called a class.

Class

noun

A blueprint for creating objects. It defines the properties (data) and methods (behaviors) that its objects will have.

For example, you could create a YouTubeVideo class. This blueprint would state that every YouTube video object must have a title, a channel name, and a duration. It could also define actions, like a Play() method.

public class YouTubeVideo
{
  // Properties (the data)
  public string Title { get; set; }
  public string ChannelName { get; set; }

  // Method (the behavior)
  public void Play()
  {
    Console.WriteLine("Playing: " + Title);
  }
}

Once the class blueprint exists, you can create actual instances, or objects, from it. Each object is its own separate entity with its own data.

// Create a new object from the YouTubeVideo class
YouTubeVideo firstVideo = new YouTubeVideo();

// Set its properties
firstVideo.Title = "C# for Beginners";
firstVideo.ChannelName = "Code Explained";

// Call its method
firstVideo.Play(); // Outputs: Playing: C# for Beginners

Handling the Unexpected

Even the best-written code can encounter problems. A user might enter text where a number is expected, or a network connection might drop. These errors are called exceptions. If you don't handle them, your application will crash.

C# provides a clean way to deal with this: the try-catch block. You put the code that might cause an error inside the try block. If an exception occurs, the program immediately jumps to the catch block, where you can handle the error gracefully.

Console.WriteLine("Enter your age:");
string input = Console.ReadLine();

try
{
  int age = int.Parse(input); // This might fail
  Console.WriteLine("You are " + age + " years old.");
}
catch (FormatException)
{
  Console.WriteLine("That's not a valid number!");
}

Using try-catch prevents your program from stopping unexpectedly and allows you to give helpful feedback to the user.

Let's review these core concepts.

Time to check your understanding.

Quiz Questions 1/6

In C#, which data type is most suitable for storing a value that can only be true or false?

Quiz Questions 2/6

What is the primary purpose of a try-catch block in C#?

These building blocks are the foundation for writing any C# application. By combining variables, control structures, objects, and exception handling, you can create powerful and reliable programs.