Wednesday, 10 July 2013

Benefits of the .NET Framework

The .NET Framework offers a number of benefits to developers:
§ A consistent programming model
§ Direct support for security
§ Simplified development efforts
§ Easy application deployment and maintenance
Consistent programming model
Different programming languages offer different models for doing the same thing. For
example, the following code demonstrates how to open a file and write a one-line
message to it using Visual Basic 6.0:
Public Sub testFileAccess()
On Error GoTo handle_Error
' Use native method of opening an writing to a file...
Dim outputFile As Long
outputFile = FreeFile
Open "c:\temp\test.txt" For Output As #outputFile
Print #outputFile, "Hello World!"
Close #outputFile
' Use the Microsoft Scripting Runtime to
' open and write to the file...
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim outputText As TextStream
Set outputText = fso.CreateTextFile("c:\temp\test2.txt")
outputText.WriteLine "Hello World!"
outputText.Close
Set fso = Nothing
Exit Sub
handle_Error:
' Handle or report error here
End Sub
This code demonstrates that more than one technique is available to create and write to
a new file. The first method uses Visual Basic's built-in support; the second method uses
the Microsoft Scripting Runtime. C++ also offers more than one way of performing the
same task, as shown in the following code:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <stdio.h>
using namespace std;
int main(int argc, char* argv[])
{
// Use the C Runtime Library (CRT)...
FILE *testFile;
if( (testFile = fopen( "c:\\temp\\test3.txt",
"wt" )) == NULL ) {
cout << "Could not open first test file!" << endl;
return 1;
}
fprintf(testFile,"Hello World!\n");
fclose(testFile);
// Use the Standard Template Library (STL)...
ofstream outputStream("c:\\temp\\test4.txt");
if(!outputStream) {
cout << "Could not open second test file!" << endl;
return(1);
}
outputStream << "Hello World!" << endl;
outputStream.close();
return 0;
}
What both code listings demonstrate is that when using different programming
languages, a disparity exists among the techniques that developers use to perform the
same task. The difference in techniques comes from how different languages interact
with and represent the underlying system that applications rely on, thereby increasing
the amount of training that developers need. The following code demonstrates how to
perform the same tasks in Visual Basic .NET and Visual C# .NE T.
Visual Basic .NET:Imports System.IO
Imports System.Text
Module Demo
Sub Main()
Dim outputFile As StreamWriter = _
New StreamWriter("c:\temp\test5.txt")
outputFile.WriteLine("Hello World!")
outputFile.Close()
End Sub
End Module
Visual C# .NET:
using System.IO;
using System.Text;
class Demo {
static void Main() {
StreamWriter outputFile =
new StreamWriter("c:\\temp\\test6.txt");
outputFile.WriteLine("Hello World!");
outputFile.Close();
}
The preceding code demonstrates, apart from slight syntactical differences, that the
technique for writing to a file in either language is identical — both listings use the
StreamWriter class to write the "Hello World!" message out to the text files. In fact,
unlike the Visual Basic and Visual C++ listings, which demonstrate that there's more
than one way to do something within the same language, the preceding listings show
that there's a unified means of accomplishing the same task by using the .NET Class
Library.
The .NET Class Library is a key component of the .NET Framework — it is sometimes
referred to as the Base Class Library (BCL). The .NET Class Library contains hundreds
of classes you can use for tasks such as the following:
§ Processing XML
§ Working with data from multiple data sources
§ Debugging your code and working with event logs
§ Working with data streams and files
§ Managing the run-time environment
§ Developing Web services, components, and standard Windows applications
§ Working with application security
§ Working with directory services
The functionality that the .NET Class Library provides is available to all .NET languages,
resulting in a consistent object model regardless of the programming language
developers use.

No comments:

Post a Comment