Translate

Pages

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, 26 March 2012

class


Class

The most common definition states that a class is a template for an object. Suppose that someone builds a paper pattern for a shirt. All the shirts done with the same paper pattern will be identical (same design, size, etc.). In this sample, the paper pattern is the class and the shirt is the object. To build the same exact shirt over and over, you need the paper pattern as a template. Another great example are house plans and blueprints. The plans and blueprints define the number of rooms, the size of the kitchen, the number of floors, and more. In this real world sample, the house plans and blueprints are the class and the house is the object. In OOP you program a class as a template for a specific object or groups ob objects that will always have the same features.

Class members

A class has different members, and developers in Microsoft suggest to program them in the following order:




Assemblies


Assembly Structure:

An assembly consists of assembly metadata describing the complete assembly, type metadata describing the exported types and methods, MSIL code, and resources. All these parts can be inside one file or spread across several files.

In the following eg, the assembly metadata, type metadata, MSIL code, and resources are all in one file – component.dll, the assembly consists of a single file.

Component.dll









Assembly Metadata

Type Metadata


IL Code


Resources



 Assembly meta data also references a module call util.netmodule, which itself includes only type meta data and MSIL code for a class. A module has no assembly metadata. Thus the module itself has no version information; it also cannot be installed separately. All the three files in this ex make up a single assembly. The assembly is the installation Unit.

NAMESPACES, ASSEMBLIES, AND COMPONENTS
How does a namespace fit into the assembly concept? The namespace is completely independent of an assembly. You can have different namespaces in a single assembly, but the same namespace can spread across assemblies. The namespace is just an extension of the type name

Viewing Assemblies
Assemblies can be viewed using the command line utility ildasm, the MSIL disassembler. An assemply can be opened by starting ildasm from the command line, with the assembly as argument or by selectin ghte menu File-open.

Understanding Strong assembly names

All assemblies contain a name, version, and a culture. However assemblies can contain a public key. An assembly containing all four pieces of information is said to be strongly named.  Only assemblies that contain strong names can be stored in the global assembly.GAC  is a disk based collection of .NET assemblies that can be accessed by any piece of .NET code on the machine containing the GAC. You can place assemblies with different strong names side by side in the global assembly cache, even if the segment names match. For example version 1.0.0.0 of an assembly named assembly.dll can be installed in GAC along with version 2.0.0.0

Sunday, 25 March 2012

BinarySearch, Sort And Reverse Method of ArrayList in C#


The ArrayList class provides a number of properties and methods that are used to work with an ArrayList.


Introduction
The ArrayList class provides a number of properties and methods that are used to work with an ArrayList. In this article, I'm going to describe three important methods of the ArrayList class, which are:
  • BinarySearch(): Performs a binary search on the ArrayList.
  • Sort(): Sorts the items in the ArrayList.
  • Reverse(): Reverses items in the ArrayList.
Namespace: System.Collections.Generic

Assembly: mscorlib (in mscorlib.dll)
BinarySearch()
The BinarySearch method of the ArrayList class implements the binary searching algorithm. This uses a "divide and conquer" approach to finding the correct element, and only works on a pre-sorted array. For this reason, never use BinarySearch if your ArrayList might not be sorted.
The BinarySearch method returns the 0-based index of the object if it's found. If the object is not found in the list, BinarySearch returns รข€“1.


How to Handle a Custom Exception in C#

By using custom exceptions, we can create and handle our own user defined exceptions. 



Introduction of custom Exception


The exceptions are anomalies that occur during the execution of a program.Exception handling is a mechanism in .NET framework to detect and handle run time errors. They can be because of user, logic or system errors. If a user (programmer) does not provide a mechanism to handle these anomalies, the .NET runtime environment provides a default mechanism, which terminates the program execution.
In C# there are three keywords TryCatch and Finally for handling exceptions.
In try block statements it might throw an exception whereas catch handles that caused by try block if one exists.
The finally block is used for doing any clean up process. The statement in finally block always executes.
e.g.
try
{
     // this can cause an exception.
}
catch (Type x)
{
    // for handling the exception.
}
finally
{
    //this will execute.
}




How to Play With Enum in C#


Step 1: Sample enum variables to use:
internal
 enum MicrosoftOffice{
    Word = 0,
    Excel = 1,
    Powerpoint = 3,
    Visio = 5
}
Step 2: 

How to get enum variable name by its value in C# ?

     Retrieves the name of the constant in the specified enumeration that has the specified value.         /// This is used to get Enum name by its value as interger.   
        private static void GetEnumName()
        {
            int[] enumValues = new int[] { 0, 1, 3, 5 };
            foreach (int enumValue in enumValues)
            {
                Console.WriteLine(Enum.GetName(typeof(MicrosoftOffice), enumValue));
            }
            Console.ReadKey();
        }
Output







Difference between struct and classes


At some point, a developer had to make a decision choosing between a struct or a class. Both of these types can be used to provide similar data structure and perform operations. 
Here is a list of the differences between the two.



CRUD Operations



CRUD Operations With Stored Procedures Via Entity Data Model Framework (EDM)

let's play around with one of the good interesting features, stored procedures with EDM Framework.

Let's get this started now.


Create a New Database and Table in SQL SERVER 2008 .


Step 1:
 Database Name: Candidate.

Step 2:  
Table Name: Student.

The Design View of the Student Table looks like this:
 
Let's now work towards creating some SP's.


C# .Net : HashTable Class



The use of a HashTable collection with examples in C#


Introduction to hashtable


A Hashtable is a collection that stores (Keys, Values) pairs. Here, the Keys are used to find the storage location and is immutable and cannot have duplicate entries in the Hashtable. The .Net Framework has provided a Hash Table class that contains all the functionality required to implement a hash table without any additional development. The hash table is a general-purpose dictionary collection. Each item within the collection is a DictionaryEntry object with two properties: a key object and a value object. These are known as Key/Value. When items are added to a hash table, a hash code is generated automatically. This code is hidden from the developer. All access to the table's values is achieved using the key object for identification. As the items in the collection are sorted according to the hidden hash code, the items should be considered to be randomly ordered.



 
Twitter Bird Gadget