Title: .NET Framework and C
1.NET Framework and C
2Outline
- Previous State of Affairs
- .NET Framework
- What C Brings to the Table
- Visual Studio .NET
3Life As a C/Win32 API Programmer
- Traditional software development for the Windows.
- C developers are forced to contend with complex
memory management and pointer arithmetic. - It lacks the benefits provided by the
object-oriented approach - When you combine the thousands of global
functions and data types defined by the Win32 API
to an already formidable language, it is little
wonder that there are so many buggy applications
floating around today.
4Life As a C/MFC Programmer
- C is an object-oriented layer on top of C.
- Programmers benefit from the famed pillars of
OOP (encapsulation, inheritance, and
polymorphism). - Microsoft Foundation Classes (MFC) provides a set
of C classes that facilitate the construction
of Win32 applications. - It wraps a sane subset of the raw Win32 API
behind a number of classes, magic macros, and
numerous code-generation tools (wizards). - Regardless of the helpful MFC, programming for
Windows using C remains a difficult and
error-prone experience.
5Visual Basic 6.0 Programmer
- VB6 is popular due to its ability to build
complex user interfaces, code libraries, and data
access logic with minimal fuss and bother. - Even more than MFC, VB6 hides the complexities of
the raw Win32 API from view using integrated code
wizards, intrinsic data types, classes, and
VB-specific functions. - The downfall of VB6 (rectified given the advent
of Visual Basic .NET) is that it is not fully
object-oriented - VB6 does not allow the programmer to establish
is-a relationships between types (i.e., no
classical inheritance) - VB6 does not provide the ability to build
multithreaded applications unless you are willing
to drop down to low-level Win32 API calls (which
is complex at best and dangerous at worst).
6Life As a Java/J2EE Programmer
- The platform independent Java programming
language is object oriented and has its syntactic
roots in C. - Java cleans up many unsavory syntactical aspects
of C. - Java provides programmers with a large number of
predefined packages that contain various type
definitions. - While Java does provide a limited ability to
access non-Java APIs, there is little support for
true cross-language integration. - Pure Java is not appropriate for many graphically
or numerically intensive applications (in these
cases, you may find Javas execution speed leaves
something to be desired). - A better approach for such programs would be to
use a language such as C where appropriate.
7Life As a COM Programmer
- The Component Object Model (COM) was Microsofts
previous application development framework. - If you build your classes in accordance with the
rules of COM, you end up with a block of reusable
binary code. - C programmers can build COM classes that can be
used by VB6. Delphi programmers can use COM
classes built using C. - COMs language independence is limited.
- There is no way to derive a new COM class using
an existing COM class (as COM has no support for
classical inheritance). - Rather, you must make use of the more cumbersome
has-a relationship to reuse COM class types. - COM is extremely complex under the hood
- The Active Template Library (ATL) provides a set
of C classes, templates, and macros to ease the
creation of COM types.
8Life As a Windows DNA Programmer
- Microsoft has been adding more Internet-aware
features into its family of operating systems and
products. - COM-based Windows Distributed interNet
Applications Architecture (DNA) is quite complex. - Due to the simple fact that Windows DNA requires
the use of numerous technologies and languages
(ASP, HTML, XML, JavaScript, VBScript, COM(),
and data access API like ADO). - The problem is that many of these technologies
are completely unrelated from a syntactic point
of view. - JavaScript has a syntax much like C, while
VBScript is a subset of VB6. The result is a
highly confused mishmash of technologies. - Each language and/or technology has its own type
system An int in JavaScript is not quite the
same as an Integer in VB6.
9Overview of the .NET Framework
- The .NET Framework is designed as an integrated
environment for seamlessly developing and running
applications on the Internet, on the desktop as
Windows Forms, and even on mobile devices - To provide a consistent object-oriented
environment across the range of applications. - To provide an environment that minimizes the
versioning conflicts ("DLL Hell") that has
bedeviled Windows (COM) programmers, and to
simplify the code distribution/installation
process. - To provide a portable environment, based on
certified standards, C and a major part of the
.NET runtime, the Common Language Infrastructure
(CLI), that can be hosted by any operating
system. - To provide a managed environment in which code is
easily verified for safe execution.
10.NET is the result of many influences
OOP
JVM
GUI
.NET
Web
component-based design
n-tier design
11.NET is multi-language
- .NET supports VB, C (C-sharp), C, J (Java
1.2), etc.
code.vb
code.cs
code.cpp
...
FCL
Development Tools
app.exe
12.NET is cross-platform
- Compiled .NET apps run on any supported platform
APP.exe
?
Win32 (XP,2K,98)
Win64
WinCE
13How is cross-platform achieved?
- Cross-platform execution realized in two ways
- Apps are written against Framework Class Library
(FCL), not underlying OS - Compilers generate generic assembly language
which must be executed by the Common Language
Runtime (CLR)
- Framework Class Library
- 1000's of predefined classes
- common subset across all platforms languages
- networking, database access, XML processing, GUI,
Web, etc. - Goal? FCL is a portable operating system
14The Common Language Runtime (CLR)
- The CLR defines a common programming model and a
standard type system for cross-platform,
multi-language development. - All .NET-aware compilers generate Intermediate
Language (IL) instructions and metadata. - The runtime's Just-in-Time (JIT) compiler convert
the IL to a machine-specific (native) code when
an application actually runs. - Because the CLR is responsible for managing this
IL, the code is known as managed code.
15Implications of .NET's Execution Model
- Clients need CLR FCL to run .NET apps
- available via Redistributable .NET Framework
- 20MB download
- runs on 98 and above, NT (sp6a) and above
- Design trade-off
- managed execution (memory protection, verifiable
code, etc.) - portability
- slower execution?
16CTS Data Types
- Language keywords map to common CTS classes
17The Common Language Specification (CLS)
- Not all languages support all CTS types and
features - C is case sensitive, VB.NET is not
- C supports pointer types (in unsafe mode),
VB.NET does not - C supports operator overloading, VB.NET does not
- CLS was drafted to promote language
interoperability - vast majority of classes within FCL are
CLS-compliant
18.NET Application Design
- Monolithic app all source code compiled into
one .EXE - not the norm on Windows
APP.exe
- Component-based app .EXE one or more .DLLs
- standard practice on Windows
compute.dll
GUI.exe
data.dll
- team programming
- multi-language development (I like C, you like
C, he/she likes VB) - code reuse (e.g. across different .EXEs)
- independent updating (update just component X)
- FCL ships as a set of components!
19What C Brings to the Table
- C is a hybrid of numerous languages, which is as
syntactically clean as Java, is about as simple
as VB6, and provides just about as much power and
flexibility as C. - C looks very similar to the syntax of Java. Both
C and Java are based on the syntactical
constructs of C. - Like VB6, C supports properties (as opposed to
traditional getter and setter methods) and the
ability to declare methods taking varying number
of arguments (via parameter arrays). - Like C, C allows you to overload operators
without the complexity (e.g., no need for return
this to allow chaining), as well as to create
callback functions (via delegates). - No pointers required! C programs typically have
no need for direct pointer manipulation. - Automatic memory management through garbage
collection. Given this, C does not support a
delete keyword. - Full support for interface-based programming
techniques and aspect-oriented programming (AOP)
techniques via attributes.
20What C Brings to the Table
- C includes useful new features
- Generics
- Partial Classes
- Member Variable Initialization
- The foreach Loop
- The enhanced switch Statement
21What C Brings to the Table
- Generics
- introduce the concept of type parameters, which
make it possible to design classes and methods
that defer the specification of one or more types
until the class or method is declared and
instantiated.
public class GenericListltTgt void Add(T
input) class TestGenericList private
class ExampleClass static void Main()
// Declare a list of type int
GenericListltintgt list1 new GenericListltintgt()
// Declare a list of type string
GenericListltstringgt list2 new
GenericListltstringgt() // Declare a list
of type ExampleClass GenericListltExampleCl
assgt list3 new GenericListltExampleClassgt()
22What C Brings to the Table
- Partial Classes
- It is possible to split the definition of a class
or a struct, or an interface over two or more
source files. - Each source file contains a section of the class
definition, and all parts are combined when the
application is compiled.
public partial class Employee public void
DoWork() public partial class
Employee public void GoToLunch()
23Member Variable Initialization
- C allows to assign member variable to an initial
value at the time of declaration - Note that C does not allow you to do so.
- It is useful when you dont want to accept the
default values and would rather not write the
same initialization code in each constructor.
class Test public int myInt 9 public
string myString I am A String. public
SportsCar viper new SportsCar(Color.Red)
...
24The foreach Loop
- Specialized foreach loop provided for collections
like array - without the need to test for the arrays upper
limit. - reduces risk of indexing error
- provides read only access
static void Main(string args) string
books "Complex Algorithms",
"Do you Remember Classic COM?",
"C and the .NET Platform" foreach (string
s in books) Console.WriteLine(s) int
data 1, 2, 3, 4, 5 int sum 0
foreach (int x in data) sum x
value
type
collection
- foreach can iterate over system-supplied or
user-defined collections
25The switch Statement
- Evaluate string data in addition to numeric data.
- no need to parse the user data into a numeric
value
static void Main(string args)
Console.WriteLine("C or VB")
Console.Write("Please pick your language
preference ") string langChoice
Console.ReadLine() switch (langChoice)
case "C" Console.WriteLine("Good choice,
C is a fine language.") break case
"VB" Console.WriteLine("VB .NET OOP,
multithreading and more!") break
default Console.WriteLine("Well...good
luck with that!") break
26Visual Studio .NET
- Refactoring is a fancy term that is used to
encompass the many ways to change the structure
and content of the code. - The Refactor menu in Visual Studio 2005 has a
range of options for renaming, extracting,
encapsulating and restructuring parts of your
application's code
27Visual Studio .NET
- Surround With in the right-click pop-up context
menu opens a list of options for you to select. - You can surround the selected block of code lines
with a compiler if block, a collapsible region
block, or any one of a number of program code
constructs - For a C project, this list includes class or
enum definitions for, foreach, do or while loop
constructs a using block and try, catch and
finally blocks.
28Visual Studio .NET
- Insert Snippets option opens a list of the
available snippets that include different kinds
of loops, a switch statement, try, catch and
finally blocks, and so forth. - Select a relatively simple code snippet foreach
from the list construct. - Notice how it includes placeholders for the
variables and types that you must enter (shaded
green in the screenshot). - As you press the Tab key, the input cursor moves
through these placeholders, and the IntelliSense
feature makes it easy to enter the required types
and variable names as you go.