Professional Documents
Culture Documents
■ 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.
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.
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.