• Provide enough information to allow you to follow the code samples • Highlight key differences with Java • Tell you where you can get the compilers • Tell you where to go for more det
Trang 1A Brief Introduction to C#
David Buksbaum
Trang 3• Provide enough information to allow you to
follow the code samples
• Highlight key differences with Java
• Tell you where you can get the compilers
• Tell you where to go for more details on C#
• Tell you where to go for more detailed
comparisons with Java
• Not to debate which is better, more important,
faster, slower, or looks better in emacs
Trang 4Quick Glossary
• BCL – Base Class Library
• CLR – Common Language Runtime
• GUI – Graphic User Interface
• MSIL – Microsoft Intermediate Language
• MS – Microsoft
• SCM – Service Control Manager
• SOA – Service Oriented Architecture
Trang 5• NET is:
• Microsoft’s Platform for Windows Development
• CLR – the Virtual Machine that runs MSIL aka MS
Byte Code
• BCL aka NET Framework
• A set of compilers that can generate MSIL C#,
Visual Basic, C++, Java (the MS flavor)
• There are 50+ languages that generate MSIL
• http://www.dotnetpowered.com/languages.aspx
• Most interoperate with each other
Trang 8A Sample C# Application
Trang 9Application Types
• Console Application
• Has standard streams (out, in, err)
• GUI can be added manually
• Windows Application
• GUI based
• No standard streams (out, in, err)
• Main thread is shared by the GUI message pump & your
code
• Service
• No standard streams (out, in, err)
• Main thread is commandeered by the SCM
• No GUI
Trang 10Compiler Options from MS
• SDK contains the command line compiler
(C:\WINDOWS\Microsoft.NET\Framework\
{version}\csc.exe)
• {version} looks like v2.0.50727
• Express Edition – Free IDE to work with Visual
C#
• Reduced runctionality version of Visual Studio
• Visual Studio – The full development system
• Optimizations, Data Access, Multi-Language, etc
• $$$
Trang 11Options Beyond MS
• Mono
• Open source development SDK for NET
• Windows, Linux, Mac OS X, Solaris, Unix
Trang 12Quick Comparison to Java
• What’s the same
Trang 13What’s Different & Relevant
Trang 14What’s Different & Relevant – cont.
• using newtypename = namespace.type;
• Directory structure != namespace hierarchy (as in
C++)
Trang 15What’s Different & Relevant – cont.
• Properties
• get, set and get/set
• public string MyProperty { get { return(_text); } set { _text =
• CompareHandler ch = new CompareHandler(myMethod);
• bool retval = ch(obj1, obj2);
Trang 16What’s Different & Relevant – cont.
• Enumerations
• public enum Protocol { UDP, TCP };
• public enum Direction { Up = 0, Down = 1, Left = 2,
Trang 17What’s Different & Relevant – cont.
• Value Types
• Primitives are the same plus
• Unsigned values
• ubyte, ushort, uint, ulong
• Careful : byte in java is sbyte in C#
• Class objects to wrap primitives
• Int32 x = new Int32(4);
• int x = Int32.Parse(“4”);
Trang 18What’s Different & Relevant – cont.
• Conversion between value type and reference type
• packet p = new packet();
• object o = (object)p; // boxed
• packet p2 = (packet)o; // unboxed
Trang 19What’s Different & Relevant – cont.
• Cross Platform Support
• NET from Microsoft is not cross platform
• It is for Windows only
• Mono can run cross platform, but is unproven in
large production environments
• MS is currently resisting moving cross platform
• The future is not set
Trang 20What’s not Relevant, but Useful
• App Domains
• One or more per process
• Represents a VM hosted logical process
• Communications between App Domains requires
marshalling (IPC)
• Assemblies
• Similar to Java JAR files
• Physically they are EXE and/or DLL files
Trang 21What’s not Relevant, but Useful
• Attributes
• Meta tags that provide run time information for a
type, NOT the instance
• Example: [Serializable] public class foo { int x; };
• [Serializable] is converted into the class
SerializableAttribute
• Attributes can be retrieved at run time
• Many framework sub-systems use attributes
• Serialization, XML, Interop, Conditional, Obsolete, etc…
Trang 22What’s not Relevant, but Useful
• Polymorphism
• Methods default to being non-virtual
• To be virtual it must be defined as virtual
• eg: public virtual int Add(int x, int y);
• To override a virtual method, you use the override
keyword
• eg public override int Add(int x, int y);
• Methods not marked virtual are equivalent to Java
final methods
• Methods can be marked with new to break the
Trang 23What’s not Relevant, but Useful
• Interop
• Access to native code through attributes
• [DllImport(“user32.dll”)] static int
GetSystemMetrics(int);
• The DllImport attribute informs the compiler and
runtime that the tagged method is inside a native
DLL.
• Options such as the actual name in the DLL,
marshalling strategies, calling convention, and more can be set using the attribute
Trang 24What’s not Relevant, but Useful
• NET 2.0 – out now
• Its all about data
• Tuples & Query constructs
Trang 25• Network information classes providing statistics, interface
information, and ping
Trang 26Future of NET Networking
• Windows Communications Foundation
• Formally code named Indigo
• Designed to make SOA an integral part of Windows
• Tight coupling with NET designs
• For more information
• http://msdn.microsoft.com/windowsvista/default.aspx?
pull=/library/en-us/dnlong/html/wcfarch.asp
Trang 27References
Trang 28• MS Visual Studio C# Express Edition
• http://msdn.microsoft.com/vstudio/express/visualcsharp/
• MS Visual Studio
• http://msdn.microsoft.com/vstudio/
Trang 31• Programming C#, Fourth Edition by Jesse
Liberty
(http://search.barnesandnoble.com/booksearch/ isbnInquiry.asp?z=y&isbn=0596006993&itm=2)
• CLR Via C#: Applied Microsoft Net Framework
2.0 Programming by Jeffrey Richter
(http://search.barnesandnoble.com/booksearch/ isbnInquiry.asp?z=y&isbn=0735621632&itm=3 )