MCS Society .NET Tech Talk Series

Download Report

Transcript MCS Society .NET Tech Talk Series

.NET Tech Talk Series
Intro to C#
Wednesday, February 5th, 2003
Michael Dunn ( [email protected] )
•Drexel University Student Ambassador to Microsoft
•Mid-Atlantic Student Ambassador Team Lead
•MCS Society Vice President
Agenda
•
•
•
•
•
•
•
•
Introduction
What is C#?
A first look at C#
Code demo
C# features
Conclusion
Questions
Raffle
Introduction
• Third in a series of .NET Technical Talks
sponsored by Microsoft and Drexel’s Math and
Computer Science Society.
• Summary of last talk:
– Keep your old C++ code, and inter-operate with .NET
using C++.NET to get access to the class library,
security, and garbage collection.
• As usual:
– Please remember to fill out evaluations – you can’t
win a door prize without a completed form
– Please hold detailed questions till the end
What is C#?
• A new object oriented language
– Syntax based on C
• Similar to C++ and Java
– Used to write .NET software
• Software that targets the .NET Framework is called managed
code
– C# gains much from the .NET Framework
•
•
•
•
•
Internet oriented platform
JIT compilation
Automatic memory management
Security, type-safety
Framework Class Library
What is C#?
• Existing languages are powerful. Why do we
need another language?
• Important features are spread out over multiple
languages
– choose between pointers (C++)
– or garbage collection (Java)
• Old languages + new features = poor syntax
– Garbage collection in C++?
– Event-driven GUIs in Java?
What is C#?
• Derived from the features and syntaxes of
other languages
– The safety of Java
– The ease of Visual Basic
– The power of C++
What is C#?
• The safety of Java
– 100% object oriented
– Automatic garbage collection
– Array bounds checking at runtime
– Strict type checking
– Structured exception handling
What is C#?
• The ease of Visual Basic
– First class support for properties
– First class support for events
– For-each loops
What is C#?
• The power of C++
– Enumerations
– Operator overloading
• Mathematical, Indexing, and Casting
– Function pointers
• Called “delegates”
• Type safe
– Structs
– Option to pass parameters by reference of by value
– Can disable type-safety, garbage collection, and
bounds checking
– Can directly manipulate memory with pointers
A first look at C#
using System.Windows.Forms;
using System.Drawing;
class MyForm:Form {
public static void Main() {
Application.Run(new MyForm());
}
protected override void OnPaint(PaintEventArgs e){
e.Graphics.DrawString("Hello World!",
new Font("Arial", 35), Brushes.Blue, 10, 100);
}
}
Code demo
What is C#?
• Most users think of two types of objects
– “Real” objects – Customer, Order, etc.
– Primitive types – int, float, bool
• Different expectations for each
– Real objects are more expensive to create
– Primitives always have a value
– Primitives have operator support
What is C#?
• Traditional views
– C++, Java: Primitive types are “magic” and do
not interoperate with objects
• C# offers improved extensibility and
reusability
– New primitive types: Decimal, SQL, …
– Collections, etc., work for all types
What is C#?
• Unified type system
– All types ultimately inherit from object
• Classes, enums, arrays, delegates, structs, …
– An implicit conversion exists from any type to
type object
object
Stream
MemoryStream
Hashtable
FileStream
int
double
What is C#?
• 76 keywords:
abstract
catch
default
explicit
foreach
internal
object
public
sizeof
this
unchecked
as
base
char
checked
delegate do
extern
false
goto
If
is
lock
operator out
readonly ref
stackalloc
throw
true
unsafe ushort
bool
class
double
finally
implicit
long
override
return
static
try
using
break
byte
const
continue
else
enum
fixed
float
in
int
namespace
params private
sbyte
sealed
string
struct
typeof
uint
virtual
void
case
decimal
event
for
interface
new
null
protected
short
switch
ulong
while
C++.NET primer (cont.)
Compilation
Source
Code
Language
Compiler
Native
Code
Code (IL)
Assembly
Metadata
JIT
Compiler
Execution
C++.NET primer
• C++.NET is _not_ a dramatically different
language from your daddy’s C++
• “Everett” C++.NET will be 98% compliant with
the ISO standard
• “Everett” will ship with /GS compiler flag to catch
buffer overflows
• The syntax generally remains the same, while
the compiler emits entirely different instructions
• Due to Intermediate Language (IL), machine
instructions are generated at runtime
C++.NET primer
• Why use C++.NET over C#, or other .NET
languages
– Use managed or unmanaged code
– Keep using your low-level or performance
critical native code alongside .NET
– Lots of code already written in C++
/clr compiler switch – “IJW” (It Just Works)
– Use cross-language debugging and all the
other neat .NET tricks
C++.NET demo
Live demo of C++.NET with
Visual Studio.NET 2002
Conclusions
• C++.NET targets the same lower-level
developers as Visual C++ 6.
• Compile your old C++ code using the
C++.NET compiler to get the advantages
of .NET.
• Moving toward ISO standardization
Next tech talk
•
•
•
•
Next week, Wednesday!
Same time, same place
Topic: Intro to C#
More books, software and food
Further references
• http://www.gotdotnet.com/team/cplusplus/
• http://msdn.microsoft.com/visualc/
• http://www.cplusplus.com/
Questions / Raffle
• Send your evaluations forward!