In MS.NET when an object is created there is no way to get the address of an object. Only the reference to the object is given through which we can access the members of the class for a given object. When an object is created all the variables (value/ reference types) are allocated the memory in a heap as a single unit and default values are set to them based on their data types.
Steps to create an Account Application: 1. Create a new project (File => New Project). Name: Account Application, Project Type: C#, Template: Windows Application 2. View =>Solution Explorer, Right Click on Project => Add => Class and name it as Account 3. To class, add the following code: Continue reading “Understanding and Programming Classes and Objects”→
This article explains the Libraries in .NET, Types of DLL, and Assemblies in .NET and its structure, Namespaces and Internal Access Specifier.
I. Introduction to Libraries
Dynamic Linking Library: A DLL is an application which can have everything that an EXE can have. The difference between them is, an EXE can execute independently but DLL cannot. The code in the DLL can be reused in many other applications but the code in EXE cannot be reused.
II. Types of DLL in Windows OS:
1. Win32 DLL: Here the code is available in the form of simple “C” functions.
2. COM DLL: This DLL has code in the form of reusable COM Components. These are also referred to as ActiveX DLL
3. .NET DLL: These DLL’s (Portable Executables) are used for distributing the reusable classes to various types of .NET applications.
• The Portable Executable (PE) format is a file format for executables, object code, DLLs, FON Font files and others used in 32-bit and 64-bit versions of Windows operating systems
• PE is a modified version of the Unix COFF file format. PE/COFF is an alternative term in Windows development.
• Microsoft’s .NET Framework has extended the PE format with features which support the Common Language Runtime (CLR).
• The CLR section of it contains two important segments: Metadata and Intermediate Language (IL) code
III. What is Assembly in .NET
An assembly is a collection of types (classes, interface, struct etc…) and resources (bmp, jpg, string table, txt, ico) that are built to work together and form a logical unit of functionality. An assembly provides the common language runtime with the information it needs to be aware of type implementations. To the runtime, a type does not exist outside the context of an assembly.
Assemblies are a fundamental part of programming with the .NET Framework.
• It contains code that the common language runtime executes.
• It forms a security boundary.
• It forms a type of boundary.
• It forms a reference scope boundary.
• It forms a version boundary.
• It forms a deployment unit.
• It is the unit at which side-by-side execution is supported.
Note: We should create a multifile assembly when you want to combine modules written in different languages and to optimize downloading an application by putting seldom-used types in a module that is downloaded only when needed
Note The files that make up a multifile assembly are not physically linked by the file system. Rather, they are linked through the assembly manifest and the common language runtime manages them as a unit.
Assembly Manifest Content:
1. Assembly Name
2. Version Number
4. Strong name information
5. List of all files in an assembly
6. Type reference information of types exported from the assembly
7. Information about other referenced assemblies
IV. About Namespaces
• A namespace is a logical collection of classes and other types with unique names. In a given namespace all the types have a unique name and thus it is used to resolve the ambiguity in case of a name conflict.
• Any type when used outside the namespace, it must be qualified by its namespace, but when used by another type within the same namespace it need not be qualified by the namespace.
• We can use using Namespace on top of the file so that we don’t have to qualify all the types of that namespace in that file explicitly.
V. Internal Access Specifier
• A public Class is accessible within and outside the assembly. Whereas an internal class can be accessed only by other types within the same assembly (irrespective of the namespace of other classes within the assembly).
• Internal is the default access specifier for the class.
• The top-level class can only be declared as either public or internal but nested class (a class within class) can have any access modifier.
• The namespace can never be used as a boundary for an access specifier. It’s used only as a name qualifier.
• An internal member of a class is accessible to all other types within the same assembly and is not accessible outside the assembly.
• A member declared as “protected internal” is accessible to all the class within the assembly and only to the derived classes outside the assembly.
• A public method of a public class cannot have either as a parameter or return type any data type of the same assembly which is not public (or is internal).
VI. Types of Assemblies:
1. Private Assemblies: An assembly whose local copy is maintained by each & every application referencing to it.
2. Shared Assembly: An assembly whose single copy deployed in Global Assembly Cache (GAC) and used/shared by many applications running on that machine. Such assemblies must have a Strong name.
3. Satellite Assembly: A resource-only assembly for a given culture is called as a satellite assembly. They don’t have any code but they have only resources like string tables and images.
4. Dynamic Assemblies: These run directly from memory and are not saved to disk before execution. You can save dynamic assemblies to disk after they have executed. The API method Reflection. Emit is used to create dynamic assemblies
You can search the profile –
2. under “People” tab
3. Type First Name “Sandeep”, Last Name “Soni”
4. click on the profile – Hit Like.
This article is all about static in C#. It explains about,
Static methods and properties
Example using all these.
The static keyword can be applied to data members, constructors, classes, methods, properties, and events. The static modifier makes an item non-instantiable.
Static members in C#
When are they loaded and how is the memory allocation,
A class is loaded when either
The static members of the class are accessed for the first time, or
When the first instance of the class is created.
A class once loaded will remain in memory permanently and also are all static members of that class.
For every new instance of a class, all the instance members are allocated memory as one unit, but static field members of a class are allocated memory only once, irrespective of the number of objects created, and they are allocated memory when the class is loaded. These members are also called class members and are accessed outside the class using the class name.
A public static member of a class can be used as a Global member of the application because it can be assessed using a class name from any part of the application.
Static Constructor in C#
It is a constructor with the static keyword.
It is used to initialize static members dynamically and is executed when the class is loaded.
It is invoked by the CLR when the class is loaded by it and hence cannot be overloaded nor can be declared with any accesses specifier like public or private.
Series of events that occur when the first object is created,
The class is loaded.
Static members are loaded and allocated memory.
The static constructor is executed.
An object is created and Instance members are loaded and allocated memory.
Instance constructor is executed.
4 and 5 repeats for a subsequent object.