You are on page 1of 5

Data Types:

■ C# has a variety of built-in data types, including int, float, double, char, bool, string,
and more. Each data type defines the kind of values a variable can hold and the operations
that can be performed on those values.
What is Variable:
■ a variable is a named storage location used to hold data that can be manipulated,
retrieved, and updated within a program.
■ Variable value can be changed dynamically by users.

Declaration and initialization:


■. Declaration tells the compiler about the variable's type and reserves memory for it.
Initialization assigns an initial value to the variable.
For example:
int age; //Declaration
age 25; // Initialization

Naming Conventions:
■ Variable names should be meaningful and follow certain naming conventions. They must
start with a letter or underscore and can be followed by letters, digits, or
underscores.Variable names are case-sensitive, so age and Age would be treated as different
variables.
■ It can be declared in Pascal Case and Camel Case both.

Scope:
■ Variables have a scope, which defines where they are accessible and usable. C# supports
block scope, meaning variables declared within a block (within curly braces ) are only
accessible within that block.

Lifetime
■ The lifetime of a variable refers to the duration it exists in memory. Local variables have a
limited lifetime, existing only within their block scope. They are created when the block is
entered and destroyed when the block is exited. On the other hand, class-level variables
(fields) exist for the lifetime of the object they belong to.

Type Inference:
■ C# also supports type inference, which allows the compiler to automatically determine the
data type of a variable based on the assigned value. This is done using the var keyword:
var name = "John"; // Compiler infers the data
type as string

Constants in C#:
Constants are variables with values that cannot be changed once assigned. They are declared
using the const keyword and must be assigned a value during declaration:
const double pi 3.14159;

Literals :
A literal is a notation used to represent a constant value directly in the source code. It's a
way to provide data directly to your program without requiring computation or processing.

Integer Literals: Whole number values, like 42 or oxFF (hexadecimal).


Floating-Point Literals: Real numbers, such as 3.14 or 1.23e-4 (scientific notation).
Character Literals: Single characters, like W, with escape sequences like ‘\n' for newlines.
String Literals: Sequences of characters, enclosed in double quotes.
Boolean Literals: Values true or false.
Null Literal: Represents absence of a value, denoted as null.
Datetime Literals: Represent date and time values.
Time Span Literals: Represent time intervals.
Verbatim String Literals: Preserve line breaks and escape characters as they are.

Type Literals: Use typeof operator to obtain a Type object for a specified type.
Type stringType = typeof(string);

Default Literal: The default keyword gets the default value for a type
int defaultValue=default:
The operator in C#:-
It is used to perform an operation using operand, operand can be variable, constant, and
literals. C# support all operators of C and C++.
c=a+b // here a and b is the variable type operand
10+20 // here 10 and 20 are the literals

Precedence of Operators in C#
The precedence of operator specifies that which operator will be evaluated first and
next. The associativity specifies the operators direction to be evaluated, it may be left to
right or right to left.

Let's understand the precedence by the example given below:

1. int data= 10+ 5*5 ;

The "data" variable will contain 35 because * (multiplicative operator) is evaluated


before + (additive operator).
The precedence and associativity of C# operators is given below:

Category (By Operator(s) Associativity


Precedence)

Unary + - ! ~ ++ -- (type)* Right to Left


& sizeof

Additive +- Left to Right

Multiplicative %/* Left to Right

Relational < > <= >= Left to Right

Shift << >> Left to Right

Equality == != Right to Left

Logical AND & Left to Right

Logical OR | Left to Right

Logical XOR ^ Left to Right

Conditional OR || Left to Right

Conditional AND && Left to Right

Null Coalescing ?? Left to Right

Ternary ?: Right to Left

Assignment = *= /= %= += - = Right to Left


<<= >>= &= ^= |
= =>

You might also like