Introduction to .Net

Introduction

The .Net framework is simply an additional layer on top of the OS whose goal is to organize its API as a set of objects, and put an end to the mess that is the Win32 API or the Unix API.

By the same token, Microsoft came up with two new languages that are C# and VB.Net which are enhanced, cleaned-up versions of C++ and VB Classic, and also tried to solve the long-going issue of distributing software, including the so-called "DLL Hell", ie. updating or letting multiple versions of a COM component coexist on the same host.

To get started, you can download the .Net SDK which includes the .Net framework per se, and the CLR compiler, or its open-source equivalents that are Mono or DotGnu (as of Sept 2004, Mono and DotGNU put much more emphasis on C#, while other .Net-based languages like VB.Net aren't going forward as fast.) You must also download the .Net framework redistributable, which is needed to compile and distribute applications; The redistributable is not included in the SDK.

But you'll get a productivity boost if you buy the VS.Net IDE, which lets you draw interfaces graphically, and test/compile in a more user-friendly environment. Other editors worth looking at are Antechinus C# Editor (commercial), and #Develop (open-source).

Temp stuff

In .NET framework when we compile a Portable executable (PE) or a component, the code is compiled in to Managed Code/Intermediate Language (MSIL or IL). This MSIL uses CPU-independent set of instructions.

The .NET Runtime ships Just-In-Time (JIT or JITter) compiler, which will convert the MSIL code in to the native code (CPU Specific code). So whatever code we write will be complied in to MSIL format and after that our code is history.

The .NET runtime/Common Language Runtime (CLR) ships three different classes of JITters. The Main JIT compiler converts the MSIL code it to native code with out any optimizations. The JIT compiler takes the MSIL code and optimizes it. So this compiler requires lot of resources like, time to compile, larger memory footprint, etc. The PreJIT is based on the Main JIT and it works like the traditional compilers rather than Just-In-Time compilers. This compiler is used at the time of installation.

Microsoft Intermediate Language (MSIL)

Common language runtime (CLR)

ASP .NET

ADO.NET

http://msdn.microsoft.com/vbasic

PyCS: This is an announcement of the beginning of development of a new Python-like language called PyCs (pronounced "pie-cees"). Like IronPython, PyCs will be Python on .Net but it will have more advanced features and probably have higher performance due to a Psyco-like implementation technique.  See http://pycs.org.  

Python.net is a python extension that allows you to call .net classes from regular python. IronPython is a python interpreter coded in either managed C or C#. (not sure which). That python interpreter not only has access to .net classes, but runs on top of the .net virtual machine.  It actually may be slightly faster than CPython (classic Python, ie. non-dotnet) since it uses clr classes which have been heavily optimized. I think it's a little early to tell about that, but it looks promising.  For right now, python.net looks to be the moderate course if you just want to call a few .net classes.

Developing

Tools

To get started if you're on a budget and don't want to buy VS.Net just yet, the easiest is to install

Hello, world!

Deployment

Other useful tools

Reading Notes

Professional VB 2005 (WROX)

 

The Book of Visual Basic 2005 by Matthew MacDonald (No Starch Press)

Programs in .NET are self-describing. In other words, when you create a .NET. exe file, it doesn’t just contain your compiled program; it also has information that describes the other components it needs in order to work, and which version of each component is supported.

Customizing the Toolbox: If a third-party developer releases a custom control that you’re eager to use, you can add it straight to the Toolbox. Just right-click the Toolbox, and select Choose Items. Select the .NET Framework Components tab, click Browse, and find the appropriate .dll file. You can then choose exactly which controls to display by adding a check mark next to the appropriate items.

The following files are created with a new application:

When hitting F5, the application will be compiled in debug mode, and stored under bin\debug. To compile in release mode, select Build > Build myproject.

The cornerstone of .NET is the common class library, which provides several thousand useful types (programming objects) that you can drop directly into a Visual Basic 2005 program. Essentially, the class library is filled with prebuilt pieces of functionality.

Every piece of code in a .NET program exists inside a conceptual construct known as a namespace. Namespaces prevent .NET from confusing one program with another. The thousands of types that are included with .NET are organized into more than 100 namespaces. Understanding how namespaces work is your key to accessing the common class library—the all-in-one repository of functionality used for everything from downloading a file from the Internet to printing a document.

In order to use any namespace in your application, you need to have access to the right assembly, which is the physical file (a .dll or .exe) that contains the corresponding compiled code. For example, if you want to use the controls in the System.Windows.Forms namespace, you need to have access to the System.Windows.Forms.dll file, where the code is stored.

In order to use the types in the class library, you need to make sure they’re accessible to your code. First you need to make sure your project has a reference to the correct assembly where the code is stored. Next, you need to consider importing the namespace that contains the types you’re interested in so that they’re easier to code with.

Once the project contains a reference to the appropriate assembly, you can access an object in a namespace by writing its fully qualified name, but this can be cumbersome, though, because many objects are stored several levels deep, in namespaces with long names. To improve on this, you can make things simpler by using the Imports statement. To see all the project-wide imports, right-click your project in the Solution Explorer and select Properties. Then click the References tab. You can also use the Imports statement to create an alias, which is typically a short form that you can use to refer to a namespace.

Quick overview of some of the more important namespaces:

The My object provides hooks into only a small subset of the total .NET Framework:

A delegate is a variable that can store a reference to a function or subroutine. You can then use this variable to execute the procedure at any time, without needing to call it directly.

Inside a class or module definition, you can add procedures and write your code as normal. However, you can’t place any type of variable or procedure declaration outside of these definitions.

Data types like Integer or String actually map to types in the System namespace (eg. System.String). Every type in Visual Basic 2005 is really a full-fledged object. Arrays in Visual Basic 2005 always start counting at element 0.

With reference types (like the array), a copy of the reference is passed for a ByVal parameter, not a copy of the actual data. This copy still refers to the same in-memory object. As a result, if you access the object and change it in some way, the change will still affect the one and only original object, contrary to what you might expect!

Visual Basic 6 forms had a dual identity, acting both as objects and as classes at the same time. In Visual Basic 2005, a form is just another class that inherits from System.Windows.Forms.Form.

Controls can be anchored so that their position adapts itself if the user resizes the window or uses a different screen definition.

The split-window interface is one of the hottest design features these days, and the applications that use it are replacing traditional MDI programs. Technically, the SplitContainer is a container control that uses two panels and includes a user-resizable splitter bar in between them. The user can drag the bar to one side or another to change the amount of space given to each panel.

Although the SplitContainer is one of the most useful containers, you’ll find several other choices in the Containers section of the Toolbox: GroupBox, Panel (a little more versatile), TabControl.

The .NET convention for events states that they must have two parameters. One, called sender, provides a reference to the object that sends the event. Thus, you can always examine the sender parameter to find out where the event originated. The other parameter, called e, is an object that bundles together any additional information that you need from the event. Different events will use different objects for e, depending on their needs.

In VB 2005, an event handler is connected to an event through the Handles keyword, which appears at the end of the definition of the event handler. This lets you change the name of the event handler without causing any problem because the actual link between an event and a control is specified explicitly with the Handles keyword. One advantage of this system is that it’s easy to create event handlers that work with more than one control.

Every Visual Basic 2005 file has the extension .vb, whether it is a form, a class, or a module. However, when you create a form VB 2005 actually creates two files. These two files are definitions for the same form class, but there’s a clear division. The file you see in the Solution Explorer contains the event handling code you’ve written. The other file is made up of the automatically generated code that initializes your form and configures its controls. To see the other side of the story, choose Project > Show All Files from the menu. Now you’ll find that every form is paired with a designer file.

Binary resources, eg. pictures, are stored in a resource file with extension .resx.

Different types of windows: dialog windows, owned forms, and MDI applications.

New types of toolbars and menus: ToolStrip, MenuStrip, ContextMenuStrip, StatusStrip, ToolStripContainer.

Providers extend the properties of other controls on the current form. For example, to add a tooltip to a control, all you need to do is drag a ToolTipProvider onto the component tray. Once you do so, every control on the current form automatically acquires a new property (courtesy of the ToolTipProvider) named ToolTip.

By default, variables inside a class are private, which means that only the code inside the class can see or change them.

VB 2005 lets you split a single class across different files. (This might be worthwhile if you’re working with extremely large classes.)

To make a property read-only, you leave out the Set procedure and add the keyword ReadOnly to the definition.

Events are notifications that an object sends to other objects. Once you have defined an event, you can fire it at a later time from inside any code using RaiseEvent. There’s no single rule to determine when you should use events and when you should do the work by explicitly calling methods in your client code.

Shared methods allow you to provide useful functionality related to a class. Many classes in the .NET class library use shared methods. For example, the System.Math class is composed entirely of useful methods for performing advanced mathematical operations. You never need to specifically create a Math object.

Just as you can create shared methods, you can create shared variables and properties by adding the Shared keyword.

Modules are usually used for helper methods—routines that perform basic conversion or utility methods that may be required in numerous different places in your application. Modules are really special classes that are made up entirely of shared members. The more you understand about Visual Basic 2005, the more you’ll realize that the language is completely built around objects and the principles of object-oriented programming. Some of the conventions of traditional Visual Basic remain, but really just as a thin veneer over the OO reality.

Visual inheritance is a strict and somewhat limiting tool. However, if you need to create several extremely similar windows, such as a series of windows for a custom wizard, you can make good use of it.

An interface contains absolutely no real code. It’s a bare skeleton that describes the members needed to support a specific feature or procedure. The difference between inheritance and interfaces is that inheritance is used to share code, whereas interfaces are used to standardize it. Interfaces guarantee that several classes all present a standard set of methods to their clients, even when the implementation details are class-specific. Some developers choose to never change anything about an interface once it’s released into the wild. They won't even make "safe" changes (like adding new methods). If they absolutely have to modify the interface, they will create a whole new interface.

Assemblies are .NET’s catch-all term for executable application files and compiled components. Assemblies are selfdescribing. Every assembly you create contains one or more program files and a manifest. The manifest includes additional information called metadata, essentially, your program code is the data, and the metadata is the information about your program, such as its name, version, publicly available types, and dependencies. The manifest replaces the type library and registry information used with COM components. All the information needed to use a component or to run an application is contained in the assembly’s manifest, which is embedded right inside the corresponding .dll or .exe file.

In .NET, private assemblies are private. You store these components in your application directory or in an application subdirectory.

The manifest also records information about the current versions of all the included files. Whenever you compile a VB 2005 program, this version information is written into the manifest automatically, meaning that there's no possibility for it to become out of date, or not be synchronized with the underlying application code. The manifest also contains a short block of cryptographic hash code based on all the files in the assembly. Whenever you run an assembly, the Common Language Runtime verifies that the hash code is valid. If a change is detected that isn’t reflected in the manifest (which is impossible, unless the file is corrupted or has been modified by a malicious user with a low-level tool), it won’t let you run the application.

Thanks to side-by-side execution, you can install multiple versions of a single component in the Global Assembly Cache. When you run an application, .NET uses the version of the component that it was developed with. No unexpected behavior or incompatibilities will appear, because every application uses the set of components that it was designed for.

To see what your program looks like as an assembly, you can use an interesting program called ILDasm.exe (IL Disassembler). If readable code is a security concern, you need to use a third-party obfuscation tool that allows you to scramble your code so that it’s difficult for humans to interpret. The full version of Visual Studio ships with a scaled-down version of one popular obfuscator, called Dotfuscator.

In .NET, it’s easy to use an auto-incrementing version number. You just need to use the asterisk (*) in your version number.

As in earlier versions of Visual Basic, a component is a collection of one or more classes that contains a set of related functions and features. These classes are provided in a .dll file, and the client can create objects based on these classes as though the class definition were part of the current project.

Instead of a simple list of assembly files, the GAC is really a complex directory structure. This structure allows multiple versions of any assembly, and uses special strong names to ensure that a name collision is impossible.

The actual assembly file is given the name you see in the GAC plug-in, but it’s stored in a special directory that uses the version number and a uniquely generated ID. You can’t copy a private assembly directly into it. Instead, you have to create a shared assembly. Before you can copy a file to the GAC, you need to create a strong name for it, ie. all the assemblies you create are signed with a special key that only you possess. This system makes it impossible for anyone else to create an assembly that pretends to be a new version of your component. The strong name you use for a shared assembly also includes an ID that is guaranteed to be statistically unique (much like a GUID).

Option Explicit stops you from using a variable without creating it, and thus prevents the mistakes that can occur when a new, empty variable is automatically created after you misspell the name of an existing variable. Option Strict prevents errors that can result from attempted automatic variable conversions.

Try
' This code could cause a problem...
Catch MyError As Exception
' We end up here if an error occurred.
Finally
' We end up here no matter what!
End Try

The Principles of Defensive Coding:

While ADO was a connection-centered database technology that threw in some disconnected access features as an afterthought, ADO.NET is based on disconnected DataSets, with no support for server-side cursors. The DataSet is able to store more than one table of data at a time, it supports table relations and column constraints, and it tracks changes seamlessly.

For situations where you need fast readonly access, and you don't want to hold onto information for longer than a few seconds, ADO.NET provides a streamlined DataReader object. A DataReader is a firehose cursor, which gets its name from the fact that it provides a steady one-way stream of data pouring from the database straight into your program. You should use a DataReader whenever you need fast, read-only data access, as its performance will always beat that of the full-fledged DataSet.

You use a DataAdapter to pull information out of the database and pop it into a DataSet, and use it again to submit DataSet changes back to the database when you’re finished.

ClickOnce is a new setup approach that sports two interesting features: installations from a website, and automatic download of updates. However, ClickOnce’s emphasis on security and simplicity means it lacks most of the snazzy installation features users of a professional application typically expect.

Q&A

Can the framework be pre-loaded at boot time and kept in RAM?

To improve performance when starting apps, is it possible to preload the .Net framework at boot time, and keept it in RAM for a few minutes after closing an application so that Windows won't have to fetch the framework from disk?

Are .Net apps hard-coded for a specific framework version?

Are .Net apps hard-coded to use a specific version of the .Net, so we must keep all versions installed on disk, or are applications version-agnostic and will simply load the latest framework installed?

Can antivirus be told to just load the framework without checking its files?

Is it possible to sign the framework and tell antivirus applications to stop checking it when loading an application?

I installed the redistributable and the SDK, but can't find the VBC.EXE compiler

C:\WINNT\Microsoft.NET

What's the diff between .Net framework and .Net framework SDK?

What is the difference between VS.Net Pro 2003, and VB.Net Standard 2003?

"Microsoft Visual Basic .NET [...] is available as a stand-alone product or as part of Visual Studio .NET. The Visual Basic .NET stand-alone product is available in the Standard Edition for new developers interested in learning the basics of .NET development. Visual Basic developers who want to build professional applications for Microsoft Windows® and the Web, build control and class libraries, and upgrade existing code should order Visual Studio .NET."

"Visual Studio .NET 2003 is available in Professional, Special Edition, Enterprise Developer, and Enterprise Architect editions. Compare the features below to see which edition best meets your development needs."

"Visual Basic 2005 Express Edition Beta: Visual Basic Express provides a lightweight experience for first-time programmers and hobbyists who are interested in building Windows Forms applications, console applications, and class libraries. Starter Kits and other introductory features make it fun for first-time programmers to take advantage of many of the productivity advances in Visual Basic 2005."

Does one need to purchase the VS.NET IDE to compile VB.NET programs?

No, the VB.NET and C# compilers come with the framework for free. Download the framework and install it, and you will get vbc.exe and csc.exe. They usually appear in the Microsoft.NET subfolders in your windows directory.

How does VB.Net improve on VB Classic?

Upgrading VB6 Projects to VB.NET ("This article describes some of the many differences between VB6 and VB.NET. Much of the current VB6 code base will not run under VB.NET due to changes in VB.NET's syntax and language, its use of the new common runtime, and its switch to WinForms from the VB6 form model.")

Resources

General

MS .Net Resources

Mono/DotGnu Resources