c# notes
c# notes
What is .NET
In simple words, .NET is a software platform developed by Microsoft that helps developers
create different types of applications more easily. It provides a collection of tools, libraries,
and a runtime environment to run programs smoothly.
• Libraries: Pre-built code to handle common tasks like data handling, web
development, and user interface design.
• Runtime (CLR): The "engine" that runs .NET programs, managing things like memory
and performance automatically.
• Cross-Platform Support: Modern versions allow you to create apps that work on
Windows, Linux, and macOS.
Developers use programming languages like C# with .NET to build apps for web, mobile,
desktop, and more.
What is C#?
• Type-Safe: Ensures that operations are performed safely and that data types are
used consistently.
• Versatile: Can be used for various types of applications, including web services,
cloud-based applications, and game development (using Unity).
4. Development Ecosystem
• Integrated Development Environment (IDE): Visual Studio and Visual Studio Code
are commonly used IDEs for C# development, offering rich features like debugging,
IntelliSense (code suggestion), and project management.
• ASP.NET: A part of the .NET platform used for developing web applications. With
ASP.NET Core, developers can build cross-platform, high-performance web
applications using C#.
• using System;
•
• namespace ExampleApp
• {
• class Program
• {
• static void Main(string[] args)
• {
• Console.WriteLine("Hello, .NET and C#!"); // Outputs:
Hello, .NET and C#!
• }
• }
• }
Literals
Literals in programming refer to fixed values that appear directly in the source code. They
represent specific data values without the need for computation or manipulation. In .NET
languages like C#, literals are used to assign fixed values to variables and constants. Here’s
a detailed breakdown of literals in C#:
1. Definition of Literals
Literals are not variables but constant values written directly in the code. For example, 5,
3.14, 'A', and "Hello" are all literals.
2. Types of Literals in C#
a. Integer Literals
• Decimal (base 10): The most common form, e.g., 42, 0, -7.
Example:
csharp
b. Floating-Point Literals
These represent real numbers with decimal points and can be of type float, double, or
decimal:
Example:
csharp
c. Character Literals
Example:
csharp
d. String Literals
• Escape Sequences: Special characters represented with a backslash, e.g., "\n" for
a new line, "\t" for a tab.
• Verbatim String Literals: Prefixed with @ to ignore escape sequences, useful for
file paths or multi-line strings, e.g., @"C:\Users\Name".
Example:
csharp
multi-line string.";
e. Boolean Literals
Example:
csharp
f. Null Literal
Example:
csharp
3. Literal Suffixes
Example:
csharp
4. Special Literals
o \n – New line
o \t – Tab
o \\ – Backslash
o \r – Carriage return
Example:
csharp
• Verbatim Strings: Starts with @, which allows using multi-line text and ignores
escape sequences.
Example:
csharp
Literals are often used to assign values to constants with the const keyword. This keyword
makes the value immutable.
Example:
csharp
In C#, a variable is a named location in memory used to store data. Variables allow
programs to store, modify, and retrieve data during execution. Each variable has a specific
type that defines the kind of data it can hold, such as integers, characters, or strings.
Syntax:
csharp
Example:
csharp
int age = 25; // Declares an integer variable named 'age' and initializes it to 25
string name = "Alice"; // Declares a string variable named 'name' and initializes it to "Alice"
bool isStudent = true; // Declares a boolean variable named 'isStudent' and initializes it to
true
csharp
int number;
2. Types of Variables in C#
Example:
csharp
b. Reference Types
These variables store references to the memory location where the data is stored.
Common reference types include:
• Interfaces.
Example:
csharp
int[] numbers = { 1, 2, 3, 4, 5 };
Example:
csharp
• Scope: Refers to the region of the code where the variable is accessible.
o Local Variables: Declared within a method or block and can only be used
there.
o Global Variables: Declared at the class level and accessible throughout the
class.
o Local Variables: Exist only during the execution of the block they are
declared in.
o Class-Level Variables: Exist as long as the instance of the class is active (for
instance variables) or for the lifetime of the application (for static variables).
Example:
csharp
class Program
Console.WriteLine(localVar);
• static: The variable belongs to the class itself, not an instance of the class.
Example:
csharp
Uninitialized variables of reference types default to null, while uninitialized value types
have specific default values:
• int: 0
• double: 0.0
• bool: false
Example:
csharp
The var keyword allows the compiler to infer the type of the variable based on the assigned
value. It provides type safety but is more concise for the programmer.
Example:
csharp
8. Nullable Types
Value types can be made nullable by adding a ? after the type, allowing them to store null
values. This is useful when you need to indicate that a variable might not have a value.
Example:
csharp
if (optionalValue.HasValue)
Console.WriteLine(optionalValue.Value);
else
Console.WriteLine("Value is null");
• Use const and readonly where applicable to prevent changes to critical values.
• Avoid global variables when possible to reduce dependencies and improve code
modularity.