1/43
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
C# SYNTAX
C# code is organized into blocks using braces {} to define scopes of classes, methods, and control structures. These rules help maintain clarity and consistency in code structure.
C# SYNTAX
is influenced by the C and C++ family of languages. Each statement in C# must end with a semicolon. The language is case-sensitive, meaning identifiers like myVariable and MyVariable are treated as distinct.
using System;
allows access to basic libraries
class Program
is a container
Main()
is the entry point of the program
identifiers
In C#, _____ are names given to variables, classes, methods, and other elements. They must start with a letter or an underscore and can contain letters, digits, and underscores.
Keywords
are reserved words in C# like int, string, if, class, and cannot be used as identifiers. Choosing meaningful identifiers improves code readability.
naming conventions
Consistent ____ are essential for readable code. In C#, it is standard to use PascalCase for class and method names, such as CalculateTotal(), and camelCase for variable names, like itemCount.
naming conventions
Constants are usually written in all uppercase letters with underscores, for example, MAX_SIZE. Following conventions helps in collaborative environments and aligns with professional practices.
DATA TYPES IN C#
C# supports several data types, which are divided into value types and reference types.
DATA TYPES IN C#
Value types, such as int, float, bool, and char, store data directly. Reference types, like string, object, arrays, and custom classes, store references to the actual data.
INTEGER TYPES
The most commonly used integer type is int, which occupies 4 bytes and can hold values between -2,147,483,648 and 2,147,483,647. Other integer types include long for larger numbers, short for smaller ranges, and byte for unsigned 8-bit numbers.
FLOATING-POINT TYPES
C# supports float, double, and decimal for real numbers. float uses 4 bytes of memory and requires an f suffix (e.g., 3.14f). double is the default type for floating-point literals and offers more precision. decimal provides even higher precision and is commonly used for financial calculations to avoid rounding errors.
bool type
The_____ can only store two values: true or false. It is commonly used in conditional statements to control program flow.
STRING TYPE
____in C# are a sequence of characters, defined using double quotes, such as "Hello World".
char type
The holds a single Unicode character and is declared using single quotes, for example, 'A'. These types are fundamental in comparison and control expressions.
STRING TYPE
The _____ is a reference type with built-in methods like .Length, .ToUpper(), .ToLower(), and .Substring(). Strings are immutable, meaning they cannot be changed once created; instead, operations return a new string.
VARIABLE DECLARATION AND INITIALIZATION
To use data in your program, you must declare variables with a type and optionally initialize them. You can also declare multiple variables in one.
CONSTANTS
are values that cannot be changed after their declaration. They are defined using the const keyword and must be initialized at the time of declaration, like const double PI = 3.14159;.
readonly
keyword is used for variables whose value is assigned only once, typically at runtime or in the constructor.
TYPE CONVERSION
C# supports implicit and explicit______ _____. Implicit conversion happens automatically when there is no risk of data loss, such as from int to float. Explicit conversion, or casting, is needed when converting from a larger type to a smaller one,
ARITHMETIC OPERATORS
operators allow you to perform calculations within your program.
ARITHMETIC OPERATORS
will assign the value 15 to result. Modulus operator % gives the remainder of division. These are frequently used in loops, conditionals, and algorithmic logic
RELATIONAL OPERATORS
____ ______ compare two values and return a boolean result. For instance, if (x > y) checks if the value of x is greater than y. These operators are vital in decision-making logic within programs, especially inside if and switch statements.
LOGICAL OPERATORS
___ _____ are used to combine multiple conditions. For example, if (x > 5 && y < 10) means the condition will be true only if both comparisons are true. Use || for OR and ! for NOT. Logical operators are crucial when handling complex conditional logic.
CONTROL STRUCTURES
_____ ______ guide the execution flow of a program. In C#, these include decision-making structures (if, else, switch) and looping structures (for, while, do-while, foreach). They allow your program to respond dynamically to input and changing conditions.
If Statement
The _ _____ is the most basic control structure. It checks a condition and executes a block of code if the condition is true. For example:
NESTED IF STATEMENT
You can check multiple conditions using else if. For instance:
SWITCH STATEMENT
The ____ ______ offers a cleaner alternative to multiple if-else statements when comparing a single variable to different constant values. Example:
FOR LOOP
A __ ___s used to repeat a block of code a specific number of times.
WHILE LOOP
A ___ ___runs as long as the condition is true. It is useful when the number of iterations is not known beforehand. Example:
DO-WHILE LOOP
The __ ____ ____ is similar to while, but the condition is checked after the loop body, ensuring the loop runs at least once. Example:
FOREACH LOOP
The ___ ____ is used to iterate over arrays or collections. It simplifies the code and avoids errors from index-based iteration. Example: