Welcome to your journey into the world of C#. This tutorial is designed to help you understand the fundamental syntax and structure of the language. Whether you're a beginner or an intermediate developer, mastering C# syntax will provide a solid foundation for building robust applications.
C# (pronounced "C Sharp") is a modern, object-oriented programming language developed by Microsoft as part of its .NET framework. It's widely used for developing desktop applications, web services, and enterprise software. Understanding the core syntax is crucial to writing effective C# code.
In this section, we'll cover some basic concepts that form the core of C# syntax:
Statements are the building blocks of C# programs. They end with a semicolon (;). For example:
1int number = 10;
This statement declares an integer variable named number and assigns it the value 10.
Comments are used to explain your code, making it easier for others (and yourself) to understand. C# supports two types of comments:
//./* */.1// This is a single-line comment23/*4This is5a multi-line6comment.7*/
Variables store data that can be manipulated during the execution of a program. In C#, you must declare variables before using them, specifying their data type.
1int age = 25;2string name = "John Doe";
In this example, age is an integer variable and name is a string variable.
C# supports various data types, including:
int, float, char, etc.1int integer = 10;2double floatingPoint = 3.14;3char character = 'A';4bool boolean = true;
Operators are used to perform operations on variables or values. C# supports arithmetic, comparison, logical, and assignment operators.
1int a = 5;2int b = 3;34int sum = a + b; // Addition5bool isEqual = a == b; // Comparison
Let's put these concepts together in some practical examples.
Here's a simple C# program that prints "Hello, World!" to the console:
1using System;23class Program4{5static void Main()6{7Console.WriteLine("Hello, World!");8}9}
This example demonstrates how to declare variables and perform operations on them:
1using System;23class Program4{5static void Main()6{7int num1 = 10;8int num2 = 5;910int sum = num1 + num2;11Console.WriteLine("Sum: " + sum);1213int difference = num1 - num2;14Console.WriteLine("Difference: " + difference);15}16}
Conditional statements allow you to execute code based on certain conditions:
1using System;23class Program4{5static void Main()6{7int number = 10;89if (number > 5)10{11Console.WriteLine("Number is greater than 5");12}13else14{15Console.WriteLine("Number is not greater than 5");16}17}18}
Now that you've learned about the core syntax of C#, it's time to dive deeper into variables. In the next section, we'll explore different types of variables, how to declare them, and their usage in C# programs.
Stay tuned for more tutorials on codingstuff.io!