130 likes | 146 Views
Learn about assemblies and namespaces in .NET applications, their importance in deployment and organization, and how to use them effectively. Explore various useful namespaces for different functionalities.
E N D
Assemblies & Namespaces Assemblies & Namespaces
Assemblies (1) • .NET applications are represented by an assembly • An assembly is a collection of all software of which the application consists • The Unit of Deployment, i.e. you need an assembly to install an application • It is normally an executable file (.exe), or a dynamic link library file (.dll) • Each assembly contains an assembly manifest, which is similar to a table of contents for the assembly. It contains information about the assembly (such as name, version), a list of files that are in this assembly, and a list of external references, etc. Assemblies & Namespaces
Assemblies (2) • Project Properties • Common Properties • Configuration Properties • Debug configuration: used by developer during development and testing, contains information for debugging • Release configuration: debugging info is removed, better performance Assemblies & Namespaces
Namespaces (1) • A software project may consist a number of pieces of code, components of an assembly • To ease the use of these components, they are grouped into to Namespace • A namespace is a group of components that are somehow related, these components can be type declarations, constants, procedures, functions, classes… • Namespaces also resolve naming ambiguity issues (two methods with same name WriteError() in different libraries) • Each assembly consists of a root namespace that contains all the components in the assembly, by default has the same name as the assembly. • New namespaces can be defined under the root namespace using the Namespace keyword Assemblies & Namespaces
NamespaceExample (1) Namespace NSEx1 Public Module ModMain Sub Main() ' test the two methods Println("Called with Println() from NSEx1") NSEx2.Println("Called with NSEx2.Println() from NSEx1") System.Console.In.ReadLine() End Sub ' Write the message to the console Public Sub Println(ByVal message As String) System.Console.Out.WriteLine("Println() in NSEx1: " & message) End Sub End Module End Namespace Assemblies & Namespaces
NamespaceExample (2) Namespace NSEx2 Public Module ModAux Public Sub Println(ByVal message As String) System.Console.Out.WriteLine("Println() in NSEx2: " & message) End Sub End Module End Namespace Assemblies & Namespaces
Two Modules are defined under two different namespaces NSEx1 and NSEx2 • Both modules have a method Println() • To access a component declared in another namespace, you need to prefix it with the name of its namespace. e.g. NSEx2.Println() • An important aspect of namespaces is that they allow only certain components to be exported and used by external code, by the Public, Private keywords. i.e. Encapsulation Assemblies & Namespaces
NamespaceExample (3) Assemblies & Namespaces
NamespaceExampleClient (1) • Add Project, Add Reference Imports NamespaceExampleLib Imports NamespaceExampleLib.NSEx2 Module Module1 Sub Main() Dim s As String Do System.Console.Out.WriteLine("Enter some text and press ENTER") System.Console.Out.WriteLine("(leave empty and press ENTER to end)") s = System.Console.In.ReadLine() NSEx1.Println(s) ' calling Println() in NSEx1 NSEx2.Println(s) ' calling Println() in NSEx2 Println(s) ' calling Println() in NSEx2, as NSEx2 is imported Loop Until s.Length = 0 End Sub End Module Assemblies & Namespaces
You can’t import Imports NamespaceExampleLib.NSEx1 Imports NamespaceExampleLib.NSEx2 • Compiler will complain Assemblies & Namespaces
Useful Namespaces (1) • System: fundamental types, utility classes • System.Collections: most commonly used collection classes such as arrays, lists, hash-tables, queues, stacks, etc. • System.Data: data manipulation when interacting with external data storage • System.Diagnostics: for debugging, testing and performance-monitoring • System.DirectoryServices: provides access to Active Directory Services • System.Drawing: graphics functions • System.IO: read/write to streams Assemblies & Namespaces
Useful Namespaces (2) • System.Messaging, System.Net: enable programs to communicate with other programs on different machines using messages and network protocols • System.Runtime: for runtime support, contains other specialized namespaces for remoting, serialization, compiler, and interoperability services • System.Security: to provide security and cryptography services • System.Text: for parsing, encoding, and decoding of text streams, and together with System.Xml namespace is used to handle XML documents Assemblies & Namespaces
Useful Namespaces (3) • System.Threading, System.Timer: offer access to operating system-level functions regarding thread handling and timer components • System.Web: for developing and deploying Web-enabled applications • System.Windows.Forms: GUI development • Microsoft.VisualBasic: contains a large number of useful enumerations, structures, functions used in Visual Basic Assemblies & Namespaces