0% found this document useful (0 votes)
45 views6 pages

C# Concepts

Learning

Uploaded by

Aseem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
45 views6 pages

C# Concepts

Learning

Uploaded by

Aseem
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

C# | Programming Language:

C# is a general-purpose, modern and object-oriented programming language


pronounced as “C Sharp”.

It is an object-oriented programming language created by Microsoft that runs


on the .NET Framework
C# is a lot similar to Java syntactically and is easy for users who have knowledge
of C, C++ or Java.
The first version was released in year 2002. The latest version, C# 8, was
released in September 2019.
An Assembly is a basic building block of .Net Framework applications. It is
basically a compiled code that can be executed by the CLR. An assembly is a
collection of types and resources that are built to work together and form a
logical unit of functionality.

Advantages:

 It is one of the most popular programming language in the world


 It is easy to learn and simple to use
 It has a huge community support
 C# is an object oriented language which gives a clear structure to
programs and allows code to be reused, lowering development costs.
 As C# is close to C, C++ and Java, it makes it easy for programmers to
switch to C# or vice versa.

Points to keep in mind:


1. CLR (Common Language Runtime) : It is a run-time environment which
executes the code written in any .NET programming language. .Net framework
provides the support for many languages like C#, F#, C++, Cobra, Jscript.Net,
VB.Net, Oxygene etc
2. FCL (Framework Class Library) : A large number of class libraries are
present in this framework which is known as FCL.
3. Types of Applications : Mainly the applications which are built in .Net
framework is divided into the following three categories :
 WinForms : Form – Based applications are considered under this
category. In simple terms, we can say client based applications which
read and writes the file system comes under this category.
 ASP .NET : Web-Based applications come under this category.
ASP.Net is a framework for web and it provides the awesome
integration of HTML, CSS and JavaScript which makes it useful to
develop the web applications, websites and web services. Web
services were added in .Net Framework 2.0 and considered as a part
of ASP.NET web applications.
 ADO .NET : It includes the application which are developed to
communicate with the database like MS SQL Server, Oracle etc.
comes. It mainly consists of classes that can be used to connect,
retrieve, insert and delete data.
CODE Structure:
using System means that we can use classes from
the System namespace.

namespace is a used to organize your code, and it is a container for classes


and other namespaces.
class is a container for data and methods, which brings functionality to your
program. Every line of code that runs in C# must be inside a class. In our
example, we named the class Program.

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.

Data types in C# is mainly divided into three categories:


 Value Data Types
 Reference Data Types
 Pointer Data Type
Value Data Types : In C#, the Value Data Types will directly store the
variable value in memory and it will also accept both signed and unsigned
literals.
Signed & Unsigned Integral Types : stores integers (whole numbers),
without decimals, such as 123 or -123 in 8-bit, 16-bit, 32-bit, and 64-bit values
in signed or unsigned form.
Floating Point Types :There are 2 floating point data types which contain the
decimal point.
Float: It is 32-bit single-precision floating point type. It has 7 digit Precision.
To initialize a float variable, use the suffix f or F. Like, float x = 3.5F;. If the
suffix F or f will not use then it is treated as double.

Double:It is 64-bit double-precision floating point type. It has 14 – 15 digit


Precision. To initialize a double variable, use the suffix d or D. But it is not
mandatory to use suffix because by default floating data types are the double
type. double myDoubleNum = 5.99D;

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.

Reference Data Types :


The Reference Data Types will contain a memory address of variable value
because the reference types won’t store the variable value directly in memory.
The built-in reference types are string, object.
String : It represents a sequence of Unicode characters and its type name
is System.String. stores text, such as "Hello World". String values are
surrounded by double quotes.
Example:
string s1 = "hello"; // creating through string keyword

String s2 = "welcome"; // creating through String class

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.

Pointer Data Type :


The Pointer Data Types will contain a memory address of the variable value.
To get the pointer details we have a two symbols ampersand (&) and
asterisk (*).
ampersand (&): It is Known as Address Operator. It is used to determine the
address of a variable.
asterisk (*): It also known as Indirection Operator. It is used to access the
value of an address.

Example:
int* p1, p; // Valid syntax

int *p1, *p; // Invalid

int n = 10;
int* p = &n;
Type Casting:
Type casting is when you assign a value of one data type to another type.

 Implicit Casting (automatically) - Converting a smaller type to a larger


type size
char -> int -> long -> float -> double

 Explicit Casting (manually) - Converting a larger type to a smaller size


type
double -> float -> long -> int -> char

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.

Methods are also called functions. These terms are used


interchangeably.Methods are extremely useful because they allow you to define
your logic once, and use it , at many places.Methods make the maintenance of
your application easier.

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

Array: It is a collection of homogeneous elements.

Collections: It is a collection of homogeneous and hectorgeneous elements.

HashTable(stores data in Key value format)


Array = ArrayList

HashTable = Dictionary

You might also like