SlideShare a Scribd company logo
1 of 28
UNIT 1
Chapter 3
C# Basics
Introduction
• Microsoft developed
• C# is a simple, modern, object oriented and
type safe programming language derived from
C and C++.
Comparing C# with java
• Same as Garbage collector
• Both are tend to intermediate language. C#-
MSIL , Java –byte code
• C# contains more primitive data types than
java
• Both supports for multidimensional
arrays.,multiple class inheritance
Comparing C# with C++
• C++ is C#’s closet relative
• C# code does not require header files. All
codes written in inline
• The C# types are different from C++
• C# statements are quite similar to C++
statements
Features of C#
• Simplicity
• Consistent Behaviour
• Modern Programming Language
• Pure Object Oriented Programming Language
• Type Safety
• Feature of Versioning
• Compatible with other Language
• Inter-operability
Identifiers and Variables
• Identifiers refer to the name of the variables,
functions, arrays, classes etc created by
programmer
• 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 and Variables
• 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.
C# Keywords
• Abstract
as
base
bool
break
byte
case
catch
char
checked
class
const
continue
decimal
default
delegate
do
double
else
Data Types
Value types
• The value data types are integer-based and
floating-point based. C# language supports both
signed and unsigned literals.
• There are 2 types of value data type in C#
language.
1.Predefined Data Types - such as Integer, Boolean,
Float, etc.
• 2) User defined Data Types - such as Structure,
Enumerations, etc.
• The memory size of data types may change
according to 32 or 64 bit operating system.
Value types
• Example
int x=10;
Int y=x;
y=20; //after this stmt x holds value 10 and y
holds value 20
Reference Types
• The reference data types do not contain the
actual data stored in a variable, but they contain
a reference to the variables.
• If the data is changed by one of the variables, the
other variable automatically reflects this change
in value.
• There are 2 types of reference data type in C#
language.
• 1) Predefined Types - such as Objects, String.
• 2) User defined Types - such as Classes, Interface.
Reference Types
• Class Demo
{
class XYZ
{ public int myValue; }
public static void main()
{ XYZ X = new XYZ();
X.myValue = 10;
XYZ Z=X;
Z.myValue = 20; //after this stmt both X.myvalue and
Z.myvalue equal to 20
}
}
Pointers
• The pointer in C# language is a variable, it is
also known as locator or indicator that points
to an address of a value.
Pointers
• Symbols:
& (ampersand sign) - Address operator -
Determine the address of a variable.
* (asterisk sign) - Indirection operator - Access
the value of an address.
Declaring a pointer
• The pointer in C# language can be declared
using * (asterisk symbol).
• int * a; //pointer to int
• char * c; //pointer to char
Type Conversion
• Type conversion is converting one type of data to
another type. It is also known as Type Casting. In C#,
type casting has two forms −
• Implicit type conversion − These conversions are
performed by C# in a type-safe manner. For example,
are conversions from smaller to larger integral types
and conversions from derived classes to base classes.
• Explicit type conversion − These conversions are done
explicitly by users using the pre-defined functions.
Explicit conversions require a cast operator.
Example
• using System;
namespace TypeConversionApplication {
class ExplicitConversion {
static void Main(string[] args) {
double d = 5673.74;
int i; // cast double to int.
i = (int)d;
Console.WriteLine(i);
Console.ReadKey();
} } }
Convert Class
• Convert base data type to another base data
type
• Convert.ToInt16(val) - Val converted to short
• Convert.ToInt32(val) - Val converted to int
• Convert.ToChar(val) - Val converted to char
• Convert.ToString(val) - Val converted to string
Parse Function
• It converts a string containing a number to
numeric
• Sbyte.Parse(str) - convert str string
representation of number in sbyte
• byte.Parse(str) - convert str string
representation of number in byte
Boxing
• Boxing is the process of converting a value
type to the type object or to any interface
type implemented by this value type.
• When the common language runtime (CLR)
boxes a value type, it wraps the value inside
a System.Object instance and stores it on the
managed heap.
• Boxing is implicit;
Boxing
• Boxing is used to store value types in the
garbage-collected heap. Boxing is an implicit
conversion of a value type to the
type object or to any interface type
implemented by this value type. Boxing a
value type allocates an object instance on the
heap and copies the value into the new
object.
Boxing
example
• In the following example, the integer
variable i is boxed and assigned to object o.
• int i = 123; // The following line boxes i. object
o = i;
UnBoxing
• Unboxing extracts the value type from the
object.
• unboxing is explicit.
• The concept of boxing and unboxing underlies
the C# unified view of the type system in
which a value of any type can be treated as an
object.
UnBoxing
• Unboxing is an explicit conversion from the
type object to a value type or from an
interface type to a value type that implements
the interface. An unboxing operation consists
of:
• Checking the object instance to make sure
that it is a boxed value of the given value type.
• Copying the value from the instance into the
value-type variable.
UnBoxing
example
• The object o can then be unboxed and
assigned to integer variable i:
o = 123;
i = (int)o; // unboxing

More Related Content

What's hot

Introduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamicIntroduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamicGieno Miao
 
Introduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh SinghIntroduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh Singhsinghadarsh
 
Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++Ankur Pandey
 
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingEelco Visser
 
Core Java Programming Language (JSE) : Chapter III - Identifiers, Keywords, ...
Core Java Programming Language (JSE) : Chapter III -  Identifiers, Keywords, ...Core Java Programming Language (JSE) : Chapter III -  Identifiers, Keywords, ...
Core Java Programming Language (JSE) : Chapter III - Identifiers, Keywords, ...WebStackAcademy
 
Datatype introduction- JAVA
Datatype introduction- JAVADatatype introduction- JAVA
Datatype introduction- JAVAHamna_sheikh
 
Java basic datatypes
Java basic datatypesJava basic datatypes
Java basic datatypesSoba Arjun
 
5variables in c#
5variables in c#5variables in c#
5variables in c#Sireesh K
 
Implementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in DottyImplementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in DottyMartin Odersky
 
C++ to java
C++ to javaC++ to java
C++ to javaAjmal Ak
 

What's hot (20)

Csc240 -lecture_4
Csc240  -lecture_4Csc240  -lecture_4
Csc240 -lecture_4
 
Java Datatypes
Java DatatypesJava Datatypes
Java Datatypes
 
Introduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamicIntroduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamic
 
C#4.0 features
C#4.0 featuresC#4.0 features
C#4.0 features
 
Introduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh SinghIntroduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh Singh
 
Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++
 
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type Checking
 
C# note
C# noteC# note
C# note
 
Core Java Programming Language (JSE) : Chapter III - Identifiers, Keywords, ...
Core Java Programming Language (JSE) : Chapter III -  Identifiers, Keywords, ...Core Java Programming Language (JSE) : Chapter III -  Identifiers, Keywords, ...
Core Java Programming Language (JSE) : Chapter III - Identifiers, Keywords, ...
 
Datatype introduction- JAVA
Datatype introduction- JAVADatatype introduction- JAVA
Datatype introduction- JAVA
 
Value Types
Value TypesValue Types
Value Types
 
Datatype
DatatypeDatatype
Datatype
 
Data types
Data typesData types
Data types
 
Java basic datatypes
Java basic datatypesJava basic datatypes
Java basic datatypes
 
5variables in c#
5variables in c#5variables in c#
5variables in c#
 
Implementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in DottyImplementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in Dotty
 
Pc module1
Pc module1Pc module1
Pc module1
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
C++ to java
C++ to javaC++ to java
C++ to java
 
Classroom Object Oriented Language (COOL)
Classroom Object Oriented Language (COOL)Classroom Object Oriented Language (COOL)
Classroom Object Oriented Language (COOL)
 

Similar to C# Basics

CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)Dilawar Khan
 
Learn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type ConversionLearn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type ConversionEng Teong Cheah
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)Shoaib Ghachi
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginnersophoeutsen2
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answerVasuki Ramasamy
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ LanguageWay2itech
 
A tour of C# - Overview _ Microsoft Learn.pdf
A tour of C# - Overview _ Microsoft Learn.pdfA tour of C# - Overview _ Microsoft Learn.pdf
A tour of C# - Overview _ Microsoft Learn.pdfParasJain570452
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#Dr.Neeraj Kumar Pandey
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C ProgrammingQazi Shahzad Ali
 
01 Java Language And OOP PART I
01 Java Language And OOP PART I01 Java Language And OOP PART I
01 Java Language And OOP PART IHari Christian
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingSherwin Banaag Sapin
 

Similar to C# Basics (20)

CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)
 
Learn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type ConversionLearn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type Conversion
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)
 
Csharp
CsharpCsharp
Csharp
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
C language
C languageC language
C language
 
LEARN C# PROGRAMMING WITH GMT
LEARN C# PROGRAMMING WITH GMTLEARN C# PROGRAMMING WITH GMT
LEARN C# PROGRAMMING WITH GMT
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answer
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
Aspdot
AspdotAspdot
Aspdot
 
A tour of C# - Overview _ Microsoft Learn.pdf
A tour of C# - Overview _ Microsoft Learn.pdfA tour of C# - Overview _ Microsoft Learn.pdf
A tour of C# - Overview _ Microsoft Learn.pdf
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#
 
Data Handling
Data HandlingData Handling
Data Handling
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Java introduction
Java introductionJava introduction
Java introduction
 
01 Java Language And OOP PART I
01 Java Language And OOP PART I01 Java Language And OOP PART I
01 Java Language And OOP PART I
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
C# basics...
C# basics...C# basics...
C# basics...
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# Programming
 

Recently uploaded

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 

Recently uploaded (20)

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 

C# Basics

  • 2. Introduction • Microsoft developed • C# is a simple, modern, object oriented and type safe programming language derived from C and C++.
  • 3. Comparing C# with java • Same as Garbage collector • Both are tend to intermediate language. C#- MSIL , Java –byte code • C# contains more primitive data types than java • Both supports for multidimensional arrays.,multiple class inheritance
  • 4. Comparing C# with C++ • C++ is C#’s closet relative • C# code does not require header files. All codes written in inline • The C# types are different from C++ • C# statements are quite similar to C++ statements
  • 5. Features of C# • Simplicity • Consistent Behaviour • Modern Programming Language • Pure Object Oriented Programming Language • Type Safety • Feature of Versioning • Compatible with other Language • Inter-operability
  • 6. Identifiers and Variables • Identifiers refer to the name of the variables, functions, arrays, classes etc created by programmer • 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.
  • 7. Identifiers and Variables • 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.
  • 10. Value types • The value data types are integer-based and floating-point based. C# language supports both signed and unsigned literals. • There are 2 types of value data type in C# language. 1.Predefined Data Types - such as Integer, Boolean, Float, etc. • 2) User defined Data Types - such as Structure, Enumerations, etc. • The memory size of data types may change according to 32 or 64 bit operating system.
  • 11. Value types • Example int x=10; Int y=x; y=20; //after this stmt x holds value 10 and y holds value 20
  • 12. Reference Types • The reference data types do not contain the actual data stored in a variable, but they contain a reference to the variables. • If the data is changed by one of the variables, the other variable automatically reflects this change in value. • There are 2 types of reference data type in C# language. • 1) Predefined Types - such as Objects, String. • 2) User defined Types - such as Classes, Interface.
  • 13. Reference Types • Class Demo { class XYZ { public int myValue; } public static void main() { XYZ X = new XYZ(); X.myValue = 10; XYZ Z=X; Z.myValue = 20; //after this stmt both X.myvalue and Z.myvalue equal to 20 } }
  • 14. Pointers • The pointer in C# language is a variable, it is also known as locator or indicator that points to an address of a value.
  • 15. Pointers • Symbols: & (ampersand sign) - Address operator - Determine the address of a variable. * (asterisk sign) - Indirection operator - Access the value of an address.
  • 16. Declaring a pointer • The pointer in C# language can be declared using * (asterisk symbol). • int * a; //pointer to int • char * c; //pointer to char
  • 17. Type Conversion • Type conversion is converting one type of data to another type. It is also known as Type Casting. In C#, type casting has two forms − • Implicit type conversion − These conversions are performed by C# in a type-safe manner. For example, are conversions from smaller to larger integral types and conversions from derived classes to base classes. • Explicit type conversion − These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
  • 18. Example • using System; namespace TypeConversionApplication { class ExplicitConversion { static void Main(string[] args) { double d = 5673.74; int i; // cast double to int. i = (int)d; Console.WriteLine(i); Console.ReadKey(); } } }
  • 19. Convert Class • Convert base data type to another base data type • Convert.ToInt16(val) - Val converted to short • Convert.ToInt32(val) - Val converted to int • Convert.ToChar(val) - Val converted to char • Convert.ToString(val) - Val converted to string
  • 20. Parse Function • It converts a string containing a number to numeric • Sbyte.Parse(str) - convert str string representation of number in sbyte • byte.Parse(str) - convert str string representation of number in byte
  • 21. Boxing • Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. • When the common language runtime (CLR) boxes a value type, it wraps the value inside a System.Object instance and stores it on the managed heap. • Boxing is implicit;
  • 22. Boxing • Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object.
  • 24. example • In the following example, the integer variable i is boxed and assigned to object o. • int i = 123; // The following line boxes i. object o = i;
  • 25. UnBoxing • Unboxing extracts the value type from the object. • unboxing is explicit. • The concept of boxing and unboxing underlies the C# unified view of the type system in which a value of any type can be treated as an object.
  • 26. UnBoxing • Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface. An unboxing operation consists of: • Checking the object instance to make sure that it is a boxed value of the given value type. • Copying the value from the instance into the value-type variable.
  • 28. example • The object o can then be unboxed and assigned to integer variable i: o = 123; i = (int)o; // unboxing