SlideShare a Scribd company logo
Programming in C#
Language Overview
Language Overview
CSE 459.24
Prof. Roger Crawfis
Overview
Quickly go through some of the major
differences between C++ or Java and
C#.
Overview of a C# program.
Built-in C# types.
Canonical Hello World program
My View on C#
 I have been using C# in our research for since C# 1.0 in 2004.
 I think it is a great language!!!
 However, programming languages are a dime a dozen and
somewhat insignificant to large-scale programming.
 The .NET libraries are the real time savers.
 Or stated differently – well tested, well maintained and well
documented reusable components.
 I could do everything in C, but view C# as an automatic code-
generator for many recurring design and programming patterns.
 A strongly-typed language prevents many simple typos from
being bugs.
Language
 Namespaces
 Classes
 Fields
 Properties
 Methods
 Attributes
 Events
 Interfaces (contracts)
 Methods
 Properties
 Events
 Control Statements
 if, else, while, for,
switch
 foreach
 Additional Features
 Operation Overloading
 Structs
 Enums
 Delegates
 OO Features
 Type Unification
 Inheritance
 Polymorphism
C# Program Structure
 Namespaces
 Contain types and other namespaces
 Type declarations
 Classes, struct’s, interfaces, enum’s, and delegates
 Contain members
 Members
 Constants, fields, methods, operators, constructors,
destructors
 Properties, indexers, events
 Organization
 No header files, types imported using assemblies (dll’s).
 Type reflection
Major Language Differences
 Automatic memory management
 Garbage Collection
 No pointers
 Everything inherits from System.Object
 Additional language constructs to aid in implementing
common patterns:
 Iterators – foreach and yield statements
 Data encapsulation – Properties
 Function pointers or Functors – delegates
 Observer Design Pattern – events
 Aspect-oriented programming – Attributes (loosely)
 Functional programming – LINQ and anonymous methods
Other Language Differences
 Salient points
 Conditionals must evaluate to a Boolean
 No pointers. Use the dot “.” to access both
namespaces and fields/methods.
 All fields are initialized by the CLR (zero for value
types, null for reference types).
 The switch statement does not “fall-thru” (unless
empty)
 The switch statement can take bool’s , enum’s,
integral types and strings.
 Expressions must be useful (no a==b;).
 Internal or assembly protection control.
Built-in Types
 C# predefined types
 The “root” object
 Logical bool
 Signed sbyte, short, int, long
 Unsigned byte, ushort, uint, ulong
 Floating-point float, double, decimal
 Textual char, string
 Textual types use Unicode (16-bit characters)
Common Value Types
All integral types: int, uint, long, short,
byte, …
float’s, double’s and decimal’s
Structs
 Aka - user-defined value (light-weight) types
 Can contain arbitrary data
 Non-extensible (sealed subclasses)
 System.Decimal is a struct.
MSDN Documentation
Let’s look at the MSDN documentation
for ValueType and few other types.
Namespace
Assembly
Language filters
Versions
Built-in Types - Strings
 Strings are first-class immutable objects in C#
 Behave like a value type
 Can be indexed like a char array.
 Has many methods (see System.String)
 The @ command allows specifying a string
literal spanning lines.
string s = “Hello”;
char third = s[2];
string[] split = s.Split(third);
Instance Intialization
 Must be initialized (unlike C++)
 double x; // x == 0
 string f; // f.equals(“”)
 A a; // a == null
 Fundamental difference between double and
class A?
 Reference types vs. value types
Reference Types
Classes, including user-defined classes
 Inherited from System.Object
 Transparently refers to a memory location
 Similar to pointers in other languages
 Other view is that: Everything is a pointer.
 Can be set to null memory
}fields in class A
for instance a
{
A a = new A();
A b = a;
}
a
b
Value Types
 Contain the actual value, not the location
 Inherited from System.ValueType
 Which is (logically) derived from System.Object.
 Treated specially by the runtime
 Need to be boxed in order to get a reference
memory
{
int a = 137;
int b = a;
}
a
b
137
137
Boxing and Unboxing
 Value types are not indirectly referenced
 More efficient.
 Less memory.
 In order to treat all types uniformly, a value
type needs is converted to a reference type.
 called boxing. Reverse is unboxing
{
int a = 137;
object o1 = a;
object o2 = o1;
int b = (int)o2;
}
memory
a
b
o1
o2
137
137
int
137
Differences Between Types
Copy semantics:
Polynomial a = new Polynomial();
Polynomial b = a;
b.Coefficient[0] = 10;
Console.WriteLine(a.Coefficient[0]);
int a = 1;
int b = a;
b = 10;
Console.WriteLine(a);
Copies of value types make a real copy
 Important for parameter passing
 Boxing still copies
Function Parameters
 In C++ you could pass a variable by:
 Value (creating a new copy and passing it)
 Pointer (passing in the address)
 Reference (same as pointer, less cluttered syntax).
 In C# there seems to be some confusion and
misinformation (including C# in a Nutshell) on this.
 The MSDN documentation for ref has this correct.
 If you were raised on pointers and understand this
(which you are not), then it is quite simple.
 The ref and out keywords indicate that you should pass:
 Value types – address of value type is passed.
 Reference types – address of pointer is passed.
Function Parameters
ref parameters
Use the value that is passed in
Change the value of a value type passed in
Change the instance that a reference type
points to.
A valid (or initialized) instance or value type
must be passed in.
 Null is a valid value.
Function Parameters
out parameters
Set the value of a value type passed in.
Set the instance that a reference type points
to.
Does not need to be initialized before
calling.
Compiler forces you to set a value for all
possible code paths.
Function Parameters
For variable number of parameters
public void f(int x, params char[] ar);
call f(1), f(1, ‘s’), f(1, ‘s’, ‘f’), f(1, “sf”.ToCharArray());
Must be the last argument in the list
Control Statements
 If, Else
 While
 For
 Foreach
Switch Statement
Switch statements must be expressions
that can be statically evaluated.
Restricted to primitive types, strings and
enums.
There is no fall through on switch cases
unless the case is an empty case:
Switch Statement
switch (myEmployee.Name)
{
case “Boss”:
Console.Writeline(“Your fired!”);
break;
case “Henry”:
case “Jane”:
Console.Writeline(“I need a pay raise.”);
break;
default:
Console.Writeline(“Busy working.”);
break;
}
Jump Statements
Several C# statements provide a break in
the execution:
 break – jumps out of a while or for loop or a
switch statement.
 continue – starts the next iteration of a loop.
 goto – do not use.
 return – returns out of the current method.
 throw – Causes an exception and ends the
current try block or method recursively.
Implicit Type Declarations
C# 3.0 feature
Aids readability in declaring instances of
complex types with redundant initialization:
var myStringBuilder = new StringBuilder();
var myList = new List<IDictionary<string,int>>();
Added for LINQ support (anonymous types).
Avoid using!
Note: still strongly and statically typed
Homework and Quiz
Memorize the C# keywords from the
appendix in the book that start with the
letters a-b.
Read the first two chapters of the book.
Look over the MSDN documentation for
System.String and in particular the Split
method.

More Related Content

PPT
Introduction to C#
PPT
Synapseindia dot net development
PPT
C Language fundamentals hhhhhhhhhhhh.ppt
PPTX
C#unit4
PPT
Introduction of C# BY Adarsh Singh
PPTX
Chapter 2 c#
PDF
C# quick ref (bruce 2016)
PPT
Csharp_mahesh
Introduction to C#
Synapseindia dot net development
C Language fundamentals hhhhhhhhhhhh.ppt
C#unit4
Introduction of C# BY Adarsh Singh
Chapter 2 c#
C# quick ref (bruce 2016)
Csharp_mahesh

Similar to CSharp_02_LanguageOverview_andintroduction (20)

PPTX
DOCX
C-sharping.docx
PPTX
PPTX
1. Introduction to C# Programming Langua
DOCX
Unit 1 question and answer
PPT
IntroductionToCSharppppppppppppppppppp.ppt
PPT
IntroductionToCSharp.ppt
PPT
IntroductionToCSharp.ppt
PPT
IntroductionToCSharp.ppt
PPT
Introduction toc sharp
PPTX
LEARN C# PROGRAMMING WITH GMT
PPTX
C# and .net framework
PDF
A tour of C# - Overview _ Microsoft Learn.pdf
PPTX
Notes(1).pptx
PPT
fdjkhdjkfhdjkjdkfhkjshfjkhdkjfhdjkhf2124C_2.ppt
PPT
Introduction To C#
PPT
Introduction to csharp
PPT
Introduction to csharp
PPT
Introduction to csharp
C-sharping.docx
1. Introduction to C# Programming Langua
Unit 1 question and answer
IntroductionToCSharppppppppppppppppppp.ppt
IntroductionToCSharp.ppt
IntroductionToCSharp.ppt
IntroductionToCSharp.ppt
Introduction toc sharp
LEARN C# PROGRAMMING WITH GMT
C# and .net framework
A tour of C# - Overview _ Microsoft Learn.pdf
Notes(1).pptx
fdjkhdjkfhdjkjdkfhkjshfjkhdkjfhdjkhf2124C_2.ppt
Introduction To C#
Introduction to csharp
Introduction to csharp
Introduction to csharp
Ad

More from Ranjithsingh20 (9)

PPT
Chapter-11 - OS-windows and its generations
PPT
OS_Chapter-12 simple concepts and design
PPT
CSharp_04_Events-in-C#-introduction-with-examples
PPT
CSharp_03_Properties_introductionwithexamples
PPTX
CSharp_03_Inheritance_introduction_with_examples
PPTX
CSharp_03_Generics_introduction_withexamples
PPT
CSharp_03_ClassesStructs_and_introduction
PPT
CSharp_02_Arrays_fundamentals_concepts_introduction
PPT
CSharp_01_CLROverview_and Introductionc#
Chapter-11 - OS-windows and its generations
OS_Chapter-12 simple concepts and design
CSharp_04_Events-in-C#-introduction-with-examples
CSharp_03_Properties_introductionwithexamples
CSharp_03_Inheritance_introduction_with_examples
CSharp_03_Generics_introduction_withexamples
CSharp_03_ClassesStructs_and_introduction
CSharp_02_Arrays_fundamentals_concepts_introduction
CSharp_01_CLROverview_and Introductionc#
Ad

Recently uploaded (20)

PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Complications of Minimal Access Surgery at WLH
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Cell Structure & Organelles in detailed.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Presentation on HIE in infants and its manifestations
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Classroom Observation Tools for Teachers
A systematic review of self-coping strategies used by university students to ...
Complications of Minimal Access Surgery at WLH
Chinmaya Tiranga quiz Grand Finale.pdf
Final Presentation General Medicine 03-08-2024.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Institutional Correction lecture only . . .
Anesthesia in Laparoscopic Surgery in India
Cell Structure & Organelles in detailed.
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Presentation on HIE in infants and its manifestations
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
GDM (1) (1).pptx small presentation for students
human mycosis Human fungal infections are called human mycosis..pptx
VCE English Exam - Section C Student Revision Booklet
Classroom Observation Tools for Teachers

CSharp_02_LanguageOverview_andintroduction

  • 1. Programming in C# Language Overview Language Overview CSE 459.24 Prof. Roger Crawfis
  • 2. Overview Quickly go through some of the major differences between C++ or Java and C#. Overview of a C# program. Built-in C# types. Canonical Hello World program
  • 3. My View on C#  I have been using C# in our research for since C# 1.0 in 2004.  I think it is a great language!!!  However, programming languages are a dime a dozen and somewhat insignificant to large-scale programming.  The .NET libraries are the real time savers.  Or stated differently – well tested, well maintained and well documented reusable components.  I could do everything in C, but view C# as an automatic code- generator for many recurring design and programming patterns.  A strongly-typed language prevents many simple typos from being bugs.
  • 4. Language  Namespaces  Classes  Fields  Properties  Methods  Attributes  Events  Interfaces (contracts)  Methods  Properties  Events  Control Statements  if, else, while, for, switch  foreach  Additional Features  Operation Overloading  Structs  Enums  Delegates  OO Features  Type Unification  Inheritance  Polymorphism
  • 5. C# Program Structure  Namespaces  Contain types and other namespaces  Type declarations  Classes, struct’s, interfaces, enum’s, and delegates  Contain members  Members  Constants, fields, methods, operators, constructors, destructors  Properties, indexers, events  Organization  No header files, types imported using assemblies (dll’s).  Type reflection
  • 6. Major Language Differences  Automatic memory management  Garbage Collection  No pointers  Everything inherits from System.Object  Additional language constructs to aid in implementing common patterns:  Iterators – foreach and yield statements  Data encapsulation – Properties  Function pointers or Functors – delegates  Observer Design Pattern – events  Aspect-oriented programming – Attributes (loosely)  Functional programming – LINQ and anonymous methods
  • 7. Other Language Differences  Salient points  Conditionals must evaluate to a Boolean  No pointers. Use the dot “.” to access both namespaces and fields/methods.  All fields are initialized by the CLR (zero for value types, null for reference types).  The switch statement does not “fall-thru” (unless empty)  The switch statement can take bool’s , enum’s, integral types and strings.  Expressions must be useful (no a==b;).  Internal or assembly protection control.
  • 8. Built-in Types  C# predefined types  The “root” object  Logical bool  Signed sbyte, short, int, long  Unsigned byte, ushort, uint, ulong  Floating-point float, double, decimal  Textual char, string  Textual types use Unicode (16-bit characters)
  • 9. Common Value Types All integral types: int, uint, long, short, byte, … float’s, double’s and decimal’s Structs  Aka - user-defined value (light-weight) types  Can contain arbitrary data  Non-extensible (sealed subclasses)  System.Decimal is a struct.
  • 10. MSDN Documentation Let’s look at the MSDN documentation for ValueType and few other types. Namespace Assembly Language filters Versions
  • 11. Built-in Types - Strings  Strings are first-class immutable objects in C#  Behave like a value type  Can be indexed like a char array.  Has many methods (see System.String)  The @ command allows specifying a string literal spanning lines. string s = “Hello”; char third = s[2]; string[] split = s.Split(third);
  • 12. Instance Intialization  Must be initialized (unlike C++)  double x; // x == 0  string f; // f.equals(“”)  A a; // a == null  Fundamental difference between double and class A?  Reference types vs. value types
  • 13. Reference Types Classes, including user-defined classes  Inherited from System.Object  Transparently refers to a memory location  Similar to pointers in other languages  Other view is that: Everything is a pointer.  Can be set to null memory }fields in class A for instance a { A a = new A(); A b = a; } a b
  • 14. Value Types  Contain the actual value, not the location  Inherited from System.ValueType  Which is (logically) derived from System.Object.  Treated specially by the runtime  Need to be boxed in order to get a reference memory { int a = 137; int b = a; } a b 137 137
  • 15. Boxing and Unboxing  Value types are not indirectly referenced  More efficient.  Less memory.  In order to treat all types uniformly, a value type needs is converted to a reference type.  called boxing. Reverse is unboxing { int a = 137; object o1 = a; object o2 = o1; int b = (int)o2; } memory a b o1 o2 137 137 int 137
  • 16. Differences Between Types Copy semantics: Polynomial a = new Polynomial(); Polynomial b = a; b.Coefficient[0] = 10; Console.WriteLine(a.Coefficient[0]); int a = 1; int b = a; b = 10; Console.WriteLine(a); Copies of value types make a real copy  Important for parameter passing  Boxing still copies
  • 17. Function Parameters  In C++ you could pass a variable by:  Value (creating a new copy and passing it)  Pointer (passing in the address)  Reference (same as pointer, less cluttered syntax).  In C# there seems to be some confusion and misinformation (including C# in a Nutshell) on this.  The MSDN documentation for ref has this correct.  If you were raised on pointers and understand this (which you are not), then it is quite simple.  The ref and out keywords indicate that you should pass:  Value types – address of value type is passed.  Reference types – address of pointer is passed.
  • 18. Function Parameters ref parameters Use the value that is passed in Change the value of a value type passed in Change the instance that a reference type points to. A valid (or initialized) instance or value type must be passed in.  Null is a valid value.
  • 19. Function Parameters out parameters Set the value of a value type passed in. Set the instance that a reference type points to. Does not need to be initialized before calling. Compiler forces you to set a value for all possible code paths.
  • 20. Function Parameters For variable number of parameters public void f(int x, params char[] ar); call f(1), f(1, ‘s’), f(1, ‘s’, ‘f’), f(1, “sf”.ToCharArray()); Must be the last argument in the list
  • 21. Control Statements  If, Else  While  For  Foreach
  • 22. Switch Statement Switch statements must be expressions that can be statically evaluated. Restricted to primitive types, strings and enums. There is no fall through on switch cases unless the case is an empty case:
  • 23. Switch Statement switch (myEmployee.Name) { case “Boss”: Console.Writeline(“Your fired!”); break; case “Henry”: case “Jane”: Console.Writeline(“I need a pay raise.”); break; default: Console.Writeline(“Busy working.”); break; }
  • 24. Jump Statements Several C# statements provide a break in the execution:  break – jumps out of a while or for loop or a switch statement.  continue – starts the next iteration of a loop.  goto – do not use.  return – returns out of the current method.  throw – Causes an exception and ends the current try block or method recursively.
  • 25. Implicit Type Declarations C# 3.0 feature Aids readability in declaring instances of complex types with redundant initialization: var myStringBuilder = new StringBuilder(); var myList = new List<IDictionary<string,int>>(); Added for LINQ support (anonymous types). Avoid using! Note: still strongly and statically typed
  • 26. Homework and Quiz Memorize the C# keywords from the appendix in the book that start with the letters a-b. Read the first two chapters of the book. Look over the MSDN documentation for System.String and in particular the Split method.