Introduction to .NET Framework, C# (C Sharp) and Windows Forms

2017/10/05 03:59

Microsoft .NET Framework

Microsoft .NET Framework is technology for building and running .NET applications. It provides a standard library of classes and a run-time environment called Common Language Runtime (CLR).
CLR is the foundation of the .NET Framework. It manages code at execution time, memory management, thread management, remoting.
The class library is object-oriented collection of reusable types that are used to develop applications – from command-line or graphical user interfaces (GUI) to web based applications (ASP.NET Web Forms, MVC and XML based services).

Compiling and execution of .NET application

Compiling and execution of .NET application

The source code of the program can be written in .NET language preferred by the programmer. The code is compiled with respective compiler for the chosen language to IL code (for example a program written in C# will be compiled with CSC, or program written in Visual Basic will be compiled with VBC). This process generates the IL Assembly. The assembly can be executable file (.exe) or Dynamic Link Library (.dll) and consists of managed code and metadata. The second contains the information about used types, names of used classes and their methods, properties and others.
When assembly is executed it is loaded in memory by the CLR. Then CLR starts to analyze the metadata of the assembly.
JIT compiler will convert the IL code to correct native code for the current system it will run on. Then the code is executed by the processor.

C# (C Sharp) Language

C# is modern, object-oriented and type-safe programming language. In Object-Oriented Programming, a program consists of various objects that interact with each other by means of actions (methods).
The C# programs consist of one or several files with a .cs extension, which contain definitions of classes and other types. These files are compiled by the C# compiler (csc) to executable code and as a result assemblies are created, which are files with the same name but with a different extension (.exe or .dll).

Component-oriented

C# is targeted to component-oriented programming, where the software is built by combining various ready-made components and a description of the interaction logic between them.
When designing the .NET Framework and C# language, the component approach is at the deepest architectural level. The .NET Framework defines a common component model that sets the rules for building and using components for all .NET applications. Language C # supports classes, interfaces, properties, events and other means of describing the components, as well as means for their use.

All data are objects

In C# is an object-oriented programming language. It encompasses the basic principles of object-oriented programming such as data encapsulation, inheritance, and polymorphism.
In the .NET Framework, all types of data inherit System.Object and acquire some common methods, properties, and other features.
As a result, in C# all data is treated as objects. Even the primitive types, by automatic boxing and unboxing, they are converted to objects.
For example, 55.ToString() is a valid call to C# because 55 is wrapped and viewed as a System.Object object that calls the ToString() method.

Strong typing and type safety

C# is strong-typed and type-safe programming language. It uses strongly-typed pointers, which are called references. Using references instead of pointers solves the problems that arise with direct access to memory. The .NET Framework Memory Management is performed almost entirely by CLR.
C# cannot go beyond the boundaries of an array or string. In an attempt to do so, an exception can be obtained that can be intercepted and processed.
When creating a class, structure, or other C# type, the compiler does not allow to leave un-initialized data members.
Although C# does not initialize local variables automatically, the compiler warns of misuse.

Security of Arithmetic Operations

In C#, by using the reserved word checked, block codes can be separated in which arithmetic operations are checked for overflow of the types, and if that happens, OverflowException is thrown. This is very useful because, unlike C++, where such situations are causing a wrong result, C# can respond adequately to such a specific situation.

Automatic memory and resource management

The .NET Framework memory allocation and usage is automatically managed by CLR (Common Language Runtime). Value types are stored in the stack while the reference types are stored in managed heap which is handled by the garbage collector.
The memory cleanup system is part of the CLR, and its task is to release periodically the memory and resources allocated to objects that are no longer used by the application.

Wide use of exceptions

The processing of errors that may occur during the execution of the program in the .NET Framework is realized using Exceptions. The exception mechanism allows to report a problem or unexpected situation and to be able to respond adequately.

Windows Forms

With Windows Forms, applications with a rich graphical user interface are being developed. These applications are easy to develop and update, they can work when they have access to the Internet when they do not, and can access resources on the local computer, ensuring much more security.
Windows Forms has an assortment of controls that can be added to the forms: text controls, buttons, drop-down menus, radio buttons, and more. If there is no control to cover the developer's needs, Windows Forms offers the ability to create custom controls using the UserControl class.

Visual Studio Windows Forms designer
Visual Studio Windows Forms designer

Windows Forms is technology designed to work with .NET Framework, and is a set of manageable libraries that simplify frequently used application operations such as reading and writing from/to the file system. When a development environment such as Visual Studio is used, Windows Forms applications can be created to display information, require user input, and communicate with other computers on the network.

For Windows Forms, form is the visual surface on which the developer provides user information. The Windows Forms application is created by adding controls and programmed responses that reflect user actions such as mouse-click or keyboard-related events. Control is a separate element belonging to the user interface group (UI) that displays information or accepts user input information.

When a user does something in the form or one of its controls, the action generates an event. The application responds to these events according to how the programmer has written the code and processes the events when they happen.
Using Visual Studio and the dag-and-drop Windows Forms designer, Windows Forms applications are very easy to create. The selected control should be selected with the mouse cursor and pulled out and placed anywhere in the form. The designer offers features such as gridlines and snap lines for easier alignment of controls.

If you need to create custom control, the System.Drawing library provides a wide variety of lines, circles, and other forms to use to create it.