C# Basics
C# is a versatile programming language developed by Microsoft, widely used for building a variety of applications, from web to desktop and mobile. This post covers the basics of C#, including syntax, data types, and object-oriented programming concepts. Dotnet is a framework that supports C# and provides a rich set of libraries and tools for developers.
Syntax
C# syntax is similar to other C-style languages like Java and C++. Here are some basic elements:
Variables: Declared with a type followed by the variable name.
Data Types: C# has several built-in data types, including:
int
: Represents integer values.double
: Represents floating-point numbers.bool
: Represents boolean values (true
orfalse
).string
: Represents a sequence of characters.
Control Structures: C# supports various control structures, such as:
- If statements: Used for conditional execution.
- For loops: Used for iterating over a range of values.
- Switch statements: Used for selecting one of many code blocks to execute.
Functions: Defined using the
void
keyword for functions that do not return a value, or with a specific return type.Classes and Objects: C# is an object-oriented language, allowing you to define classes and create objects.
Preprocessor Directives
C# supports preprocessor directives, which are commands that are processed before the compilation of the code
- Using Directives: Used to include namespaces in your code.
1
using System;
- Conditional Compilation: Allows you to include or exclude code based on certain conditions.
Basic IO statements
C# provides several methods for input and output operations:
Console Input/Output: The
Console
class provides methods for reading from and writing to the console.- Taking in a number: You can read a number from the console and convert it to an integer.