Goglides Dev 🌱

Cover image for C# For Beginners – 5 Simplified Concepts In C#
Dev Leader
Dev Leader

Posted on • Originally published at devleader.ca

C# For Beginners – 5 Simplified Concepts In C#

The post C# for Beginners – 5 Simplified Concepts in C# appeared first on Dev Leader.

C# is my favorite programming language, and if you’re just starting out, I’m hopeful that I can be part of your learning journey! We’ll focus on C# for beginners here, so if you’re already familiar with how to write code in C# this is probably not for you. But if you’re just starting out… you’re in the right spot!

Throughout this article, I’ll guide you through five beginner concepts in C#, each with its own code example. These cover the very basics of C# to provide you with a solid foundation in C# programming. By the end of this article, you’ll not only understand these concepts but also be able to apply them in your first C# programs. And if you’d like a more guided video approach, you can check out my course on getting started in C#!

Let’s get started with these beginner C# concepts!


Beginner Concepts in C

In this section, I’ll cover five beginner concepts in C# that are important for new software engineers to understand. These concepts form the foundation of C# programming and are important for building robust and efficient applications. By understanding these concepts, you’ll be well on your way to writing your very own applications!

Variables and Data Types in CSharp

Variables are used to store and manipulate data in C#. They allow you to give names to values and refer to them later in your code. In C#, variables must have a data type, which defines the type of data that can be stored in that variable. Common data types in C# include integers (int), strings (string), and booleans (bool). Here’s an example of declaring and initializing variables:

int age = 25;
string name = "John";
bool isStudent = true;
Enter fullscreen mode Exit fullscreen mode

In the above code, we declare three variables: age of type int, name of type string, and isStudent of type bool. We also initialize them with specific values. Understanding variables and data types is important for performing calculations, manipulating data, and representing real-world information in your programs.

Conditionals and Loops in CSharp

Conditionals and loops are important “control flow” structures in C# that allow you to make decisions and repeat sections of code based on certain conditions. Conditionals, such as if statements, enable you to execute a block of code only if a certain condition is met. Loops, such as for and while loops, allow you to repeat a block of code multiple times until a condition is no longer true.

Here’s an example of using conditionals and loops in C#:

int number = 10;

if (number > 0)
{
    Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
    Console.WriteLine("The number is negative.");
}
else
{
    Console.WriteLine("The number is zero.");
}

for (int i = 0; i < 5; i++)
{
    Console.WriteLine("Iteration: " + i);
}

while (number > 0)
{
    Console.WriteLine("Number: " + number);
    number--;
}
Enter fullscreen mode Exit fullscreen mode

In the above code, we use an if statement to check whether number is positive, negative, or zero. The for loop is used to iterate five times and print the current iteration number. The while loop prints the value of number until it becomes zero. Conditionals and loops are powerful tools for controlling the flow of your program and executing code based on specific conditions.

Methods and Functions in CSharp

Methods and functions are reusable blocks of code that perform a specific task. They allow you to encapsulate functionality, improve code organization, and promote code reusability. Methods can take parameters, perform operations, and return values. In C#, methods are defined using the void keyword if they don’t return a value or a specific return type if they do.

Here’s an example of creating and calling a method in C#:

void Greet(string name)
{
    Console.WriteLine("Hello, " + name + "!");
}

int Add(int a, int b)
{
    return a + b;
}

Greet("John");
int sum = Add(2, 3);
Enter fullscreen mode Exit fullscreen mode

In the above code, we define a method Greet that takes a name parameter and prints a greeting message. We also define a method Add that takes two integer parameters a and b and returns their sum. We then call these methods with specific arguments. Methods and functions are important building blocks of C# programs and allow you to solve complex problems by breaking them down into smaller, manageable pieces.

Getting Started: C# - Dometrain Course

It's time to level up! Check out this course on Dometrain!

Arrays and Lists in CSharp

Arrays and lists are data structures used to store collections of elements in C#. Arrays have a fixed size, while lists can dynamically grow and shrink as needed. Both arrays and lists can store elements of the same type, such as integers or strings. They provide convenient ways to access, manipulate, and iterate over collections of data.

Here’s an example of using arrays and lists in C#:

int[] numbers = { 1, 2, 3, 4, 5 };
Console.WriteLine("First number: " + numbers[0]);

List<string> names = new List<string>();
names.Add("John");
names.Add("Jane");
names.Add("Alice");
Console.WriteLine("First name: " + names[0]);
Enter fullscreen mode Exit fullscreen mode

In the above code, we create an array numbers that stores five integer values. We access and print the first number using the index 0. We also create a list names that initially doesn’t contain any elements. We use the Add method to add three names to the list and access and print the first name using the index 0. One of the big differentiators between these two is that arrays are intended to be for fixed size collections and lists are designed to change in size.

Arrays and lists are fundamental data structures in C# and are used extensively in various programming tasks. I use them every single day!

Classes and Objects in CSharp

Classes and objects are the foundation of object-oriented programming (OOP) in C#. A class is a blueprint or template that defines the structure and behavior of objects, whereas an object is an instance of a class. Classes allow you to model real-world entities and define their properties (fields) and actions (methods). Objects can be created from classes and have their own unique state and behavior.

Here’s an example of creating a class and an object in C#:

class Car
{
    string brand;
    int year;

    public Car(string brand, int year)
    {
        this.brand = brand;
        this.year = year;
    }

    public void StartEngine()
    {
        Console.WriteLine("Engine started for " + brand);
    }
}

Car myCar = new Car("Toyota", 2021);
myCar.StartEngine();
Enter fullscreen mode Exit fullscreen mode

In the above code, we define a class Car with a brand field and a year field. We also define a constructor that sets the initial values of these fields. The StartEngine method starts the engine for the car and prints a message. We create an object myCar of type Car using the constructor and call the StartEngine method on it. Classes and objects allow you to create modular and reusable code in C#, enabling you to build complex systems with ease.


Wrapping Up C# For Beginners

We’ve explored five beginner concepts in C# and you’ve been provided with some simple code examples to illustrate each one. Each of these concepts help to cover some of the fundamentals of C#, and in fact, you’ll find variations of these in other programming languages as well.

We started by discussing the concept of variables and data types, highlighting the importance of properly defining and using them in C# programs. Next, we checked out control flow statements, which allow us to make decisions and repeat actions based on certain conditions. We then explored the concept of arrays and lists, which enable us to store and manipulate collections of related data items. Functions were next on our list where we learned how to encapsulate reusable code blocks. And lastly, I discussed the concept of classes and objects, which form the basis of object-oriented programming in C#.

I encourage you to continue your learning journey with C#. There is always more to explore and discover in this language, and after nearly two decades of using it, I still learn every day. If you found this useful and you’re looking for more learning opportunities, consider subscribing to my free weekly software engineering newsletter and check out my free videos on YouTube!


Want More Dev Leader Content?

  • Follow along on this platform if you haven’t already!
  • Subscribe to my free weekly software engineering and dotnet-focused newsletter. I include exclusive articles and early access to videos: SUBSCRIBE FOR FREE
  • Looking for courses? Check out my offerings: VIEW COURSES
  • E-Books & other resources: VIEW RESOURCES
  • Watch hundreds of full-length videos on my YouTube channel: VISIT CHANNEL
  • Visit my website for hundreds of articles on various software engineering topics (including code snippets): VISIT WEBSITE
  • Check out the repository with many code examples from my articles and videos on GitHub: VIEW REPOSITORY

Top comments (2)

Collapse
 
pixelpilot98 profile image
PixelPilot98

Great guide

Collapse
 
devleader profile image
Dev Leader

Glad you enjoyed it, thanks!