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

PPT
Lecture 1
PPTX
PPT
Introduction To C#
PPTX
Type checking compiler construction Chapter #6
PPT
C sharp
PPT
Type Checking(Compiler Design) #ShareThisIfYouLike
PDF
Java data types, variables and jvm
Lecture 1
Introduction To C#
Type checking compiler construction Chapter #6
C sharp
Type Checking(Compiler Design) #ShareThisIfYouLike
Java data types, variables and jvm

What's hot (20)

PPTX
Csc240 -lecture_4
PPTX
Java Datatypes
PPSX
Introduction to c sharp 4.0 and dynamic
PPTX
PPT
Introduction of C# BY Adarsh Singh
PDF
Keywords, identifiers ,datatypes in C++
PDF
Compiler Construction | Lecture 7 | Type Checking
DOCX
PDF
Core Java Programming Language (JSE) : Chapter III - Identifiers, Keywords, ...
PPTX
Datatype introduction- JAVA
PDF
Value Types
PDF
Datatype
PPT
Data types
PDF
Java basic datatypes
PPTX
5variables in c#
PDF
Implementing Higher-Kinded Types in Dotty
PDF
Pc module1
PPTX
Classes objects in java
PPT
C++ to java
PDF
Classroom Object Oriented Language (COOL)
Csc240 -lecture_4
Java Datatypes
Introduction to c sharp 4.0 and dynamic
Introduction of C# BY Adarsh Singh
Keywords, identifiers ,datatypes in C++
Compiler Construction | Lecture 7 | Type Checking
Core Java Programming Language (JSE) : Chapter III - Identifiers, Keywords, ...
Datatype introduction- JAVA
Value Types
Datatype
Data types
Java basic datatypes
5variables in c#
Implementing Higher-Kinded Types in Dotty
Pc module1
Classes objects in java
C++ to java
Classroom Object Oriented Language (COOL)
Ad

Similar to C# Basics (20)

PPTX
CS4443 - Modern Programming Language - I Lecture (2)
PDF
Learn C# Programming - Data Types & Type Conversion
PDF
2.Getting Started with C#.Net-(C#)
PDF
OOP UNIT 1_removed ppt explaining object.pdf
PPT
Csharp
PPTX
C programming tutorial for Beginner
PPTX
C language
PPTX
LEARN C# PROGRAMMING WITH GMT
DOCX
Unit 1 question and answer
PDF
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
PDF
PROGRAMMING IN C UNIT II.pdfFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
PPTX
External Data Representation and Marshalling
PPTX
C programming language:- Introduction to C Programming - Overview and Importa...
PPTX
C_plus_plus
PPTX
2. Introduction to 'C' Language (1).pptx
PPTX
Variables in C and C++ Language
PPTX
Introduction to C language programming.pptx
PPTX
PDF
A tour of C# - Overview _ Microsoft Learn.pdf
PPTX
C# lecture 2: Literals , Variables and Data Types in C#
CS4443 - Modern Programming Language - I Lecture (2)
Learn C# Programming - Data Types & Type Conversion
2.Getting Started with C#.Net-(C#)
OOP UNIT 1_removed ppt explaining object.pdf
Csharp
C programming tutorial for Beginner
C language
LEARN C# PROGRAMMING WITH GMT
Unit 1 question and answer
THE C++ LECTURE 2 ON DATA STRUCTURES OF C++
PROGRAMMING IN C UNIT II.pdfFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
External Data Representation and Marshalling
C programming language:- Introduction to C Programming - Overview and Importa...
C_plus_plus
2. Introduction to 'C' Language (1).pptx
Variables in C and C++ Language
Introduction to C language programming.pptx
A tour of C# - Overview _ Microsoft Learn.pdf
C# lecture 2: Literals , Variables and Data Types in C#
Ad

Recently uploaded (20)

PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PPTX
Climate Change and Its Global Impact.pptx
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PPTX
Computer Architecture Input Output Memory.pptx
PDF
English Textual Question & Ans (12th Class).pdf
PDF
Journal of Dental Science - UDMY (2022).pdf
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
IP : I ; Unit I : Preformulation Studies
PDF
Literature_Review_methods_ BRACU_MKT426 course material
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
Journal of Dental Science - UDMY (2020).pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
PDF
Journal of Dental Science - UDMY (2021).pdf
PPTX
Education and Perspectives of Education.pptx
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
My India Quiz Book_20210205121199924.pdf
PDF
Climate and Adaptation MCQs class 7 from chatgpt
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
Climate Change and Its Global Impact.pptx
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
Computer Architecture Input Output Memory.pptx
English Textual Question & Ans (12th Class).pdf
Journal of Dental Science - UDMY (2022).pdf
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
IP : I ; Unit I : Preformulation Studies
Literature_Review_methods_ BRACU_MKT426 course material
B.Sc. DS Unit 2 Software Engineering.pptx
Journal of Dental Science - UDMY (2020).pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
Journal of Dental Science - UDMY (2021).pdf
Education and Perspectives of Education.pptx
Unit 4 Computer Architecture Multicore Processor.pptx
My India Quiz Book_20210205121199924.pdf
Climate and Adaptation MCQs class 7 from chatgpt

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