C# Concepts
C# Concepts
Advantages:
Comments:
Single-line comments start with two forward slashes ( //).
Multi-line comments start with /* and ends with */.
XML comments shortcut(///).
Identifiers:
identifiers are used for identification purposes. Or in other words, identifiers are
the user-defined name of the program components. In C#, an identifier can be
a class name, method name, variable name or label.
Rules for defining identifiers in C#:
There are certain valid rules for defining a valid C# identifier. These rules
should be followed, otherwise, we will get a compile-time error.
The only allowed characters for identifiers are all alphanumeric
characters([A-Z], [a-z], [0-9]), ‘_‘ (underscore). For example
“geek@” is not a valid C# identifier as it contain ‘@’ – special
character.
Identifiers should not start with digits([0-9]). For example “123geeks”
is a not a valid in C# identifier.
Identifiers should not contain white spaces.
Identifiers are not allowed to use as keyword unless they include @ as
a prefix. For example, @as is a valid identifier, but “as” is not
because it is a keyword.
C# identifiers allow Unicode Characters.
C# identifiers are case-sensitive.
C# identifiers cannot contain more than 512 characters.
Identifiers does not contain two consecutive underscores in its name
because such types of identifiers are used for the implementation.
Variables:
Variable is like a container, Which contains the value(otherwise called
placeholder of the information), The thing to keep in mind that we need right
type of container(DataType) and it is variable (Varry-Able) Which means the
value of the variable can change throughout the execution of program. And it
allows to Retrieve and Manipulate the stored information.
Characteristics of Variables:
name : It must be a valid identifier. In above example, var is a valid
identifier.
type : It defines the types of information which is to be stored into
the variable. In above example char is a type.
value : It is the actual data which is to be stored in the variable. In
above example ‘h’ is the value.
Initializing Variables:
The term initializing means to assign some value to the variable. Basically,
the actual use of variables comes under the initialization part. In C# each data
type has some default value which is used when there is no explicitly set value
for a given variable. Initialization can be done separately or may be with
declaration.
Ex:
int y = 7; // Declaring and initializing the variable at same
time
int x; // Declaring variable x
x = 5; // initializing x with value 5
Two Ways for Initialization:
1. Compile time initialization | int a = 4;
2. Run time initialization | int sum = a+b;
Data Types
Data types specify the type of data that a valid C# variable can hold. C# is
a strongly typed programming language because in C#, each type of data
(such as integer, character, float, and so forth) is predefined as part of the
programming language and all constants or variables defined for a given
program must be described with one of the data types.
Value type conversion to reference type is called as Boxing.
Reference type to Value type is Unboxing.
Decimal Types : The decimal type is a 128-bit data type suitable for financial
and monetary calculations. It has 28-29 digit Precision. To initialize a decimal
variable, use the suffix m or M. Like as, decimal x = 300.5m;. If the suffix m or
M will not use then it is treated as double.
Character Types : The character types represents a UTF-16 code unit or
represents the 16-bit Unicode character.
Boolean Types : It has to be assigned either true or false value. Values of
type bool are not converted implicitly or explicitly (with casts) to any other
type. But the programmer can easily write conversion code.
Object : In C#, all types, predefined and user-defined, reference types and
value types, inherit directly or indirectly from Object. So basically it is the base
class for all the data types in C#. Before assigning values, it needs type
conversion. When a variable of a value type is converted to object, it’s
called boxing. When a variable of type object is converted to a value type, it’s
called unboxing. Its type name is System.Object.
Example:
int* p1, p; // Valid syntax
int n = 10;
int* p = &n;
Type Casting:
Type casting is when you assign a value of one data type to another type.
Keywords or Reserved words are the words in a language that are used for
some internal process or represent some predefined actions. These words are
therefore not allowed to use as variable names or objects. Doing this will result
in a compile-time error.
Generics in C# :
It allows us to write a class or method that can work with any data type. (Class,
Abstract Class, Interface, functions, static methods, events, delegates.)
EX:
Aam Zindegi- Public static void add(int[] arr)
Mentos Zindegi- public statis void add<T>(T[] arr)
Note: T is just a placeholder, You can write whatever in that place.
Code reuseability.
Performance benefit(No boxing , Unboxing is necessary)
Type safe.(In compile time the erros will be shown)
Collections in C# :
Collection is dynamic array which stores
Generic collections(system.collections.Generic):
List<T>
Dictionary<TKey, Tvalue>
Queue<T>
Non-Generic collections(system.collections.Generic):
ArrayList
Hashtable
Stack
Queue
HashTable = Dictionary