Welcome to Lesson 3! In this lesson, we’ll explore the basic syntax of C# and introduce you to its data types. Understanding the syntax and data types is crucial for writing effective and error-free code.
1. Basic Syntax
C# syntax is designed to be clean and readable. Here are some fundamental syntax elements you’ll encounter:
1.1. Comments
- Single-line comments: Start with
//
. They extend to the end of the line.
// This is a single-line comment
- Multi-line comments: Enclosed between
/*
and*/
.
/*
This is a multi-line comment
It can span multiple lines
*/
1.2. Statements and Semicolons
- Each statement in C# ends with a semicolon (
;
).
int number = 10; // This is a statement
1.3. Blocks of Code
- Code blocks are enclosed in curly braces
{}
.
class Program
{
static void Main()
{
// Code inside Main method
}
}
1.4. Indentation and Formatting
- Indentation helps make the code more readable. It’s a good practice to use consistent indentation (typically 4 spaces or a tab) to structure your code properly.
2. Data Types
C# supports various data types, which can be broadly classified into value types and reference types. Here’s a closer look at the most commonly used data types:
2.1. Value Types
- Value types store data directly and are usually stack-allocated. They include: 2.1.1. Integers
int
: 32-bit signed integercsharp int age = 30;
long
: 64-bit signed integerlong distance = 123456789L;
2.1.2. Floating-Point Numbersfloat
: Single-precision floating-point numbercsharp float temperature = 98.6f;
double
: Double-precision floating-point numberdouble pi = 3.14159;
2.1.3. Decimaldecimal
: 128-bit precise decimal value, useful for financial calculationsdecimal price = 19.95m;
2.1.4. Characters and Booleanchar
: Single 16-bit Unicode charactercsharp char initial = 'A';
bool
: Represents a Boolean value (true or false)csharp bool isCSharpFun = true;
2.2. Reference Types
- Reference types store references to objects and are typically heap-allocated. They include: 2.2.1. Strings
string
: Represents a sequence of charactersstring greeting = "Hello, World!";
2.2.2. Objectsobject
: The base type from which all other types derive; can hold any data typecsharp object anything = "Can be anything"; anything = 12345; // Now holding an integer
3. Type Conversion
You often need to convert between different data types. C# supports both implicit and explicit type conversions:
3.1. Implicit Conversion
- Automatic conversion by the compiler from a smaller type to a larger type (e.g., from
int
todouble
).
int a = 10;
double b = a; // Implicit conversion
3.2. Explicit Conversion
- Requires a cast when converting from a larger type to a smaller type or between incompatible types.
double x = 9.78;
int y = (int)x; // Explicit conversion
3.3. Using Convert
Class
- The
Convert
class provides methods for converting between base types.
string numberString = "123";
int number = Convert.ToInt32(numberString);
4. Variable Declaration and Initialization
In C#, you declare variables to store data, and you can initialize them at the time of declaration:
4.1. Declaring Variables
- To declare a variable, specify the data type followed by the variable name.
int age;
float height;
4.2. Initializing Variables
- You can initialize a variable when declaring it.
int age = 25;
float height = 5.9f;
5. Example: Combining Syntax and Data Types
Here’s a complete example that demonstrates basic syntax and data types in a C# program:
Code Example:
using System;
class Program
{
static void Main()
{
// Variable declarations and initializations
int numberOfStudents = 30;
double averageScore = 85.5;
string courseName = "Introduction to C#";
bool isPassed = true;
// Output to the console
Console.WriteLine("Course: " + courseName);
Console.WriteLine("Number of Students: " + numberOfStudents);
Console.WriteLine("Average Score: " + averageScore);
Console.WriteLine("Passed: " + isPassed);
}
}
Explanation:
- We declare variables of various types, initialize them, and then use
Console.WriteLine
to print their values to the console.
Summary
In this lesson, you learned about the basic syntax of C# and its data types. Understanding these fundamentals is essential for writing effective C# code. We covered variable declarations, data types, type conversions, and how to use these in a simple program.
Happy coding!
0 Comments