Saturday, July 20, 2013

Structures in C Sharp


                       Structures are like classes, where as we can create objects that behave like the built –in types. They resemble most like classes even though it’s found lot of differences between them.
Primitive difference between them is


  •  Structures are value based where as classes are reference types
  • Structures do not possess Deconstruct  characteristics
  •  Inheritance is strictly not supported in structures where as in classes we do.
  •   Structures do not support OOPS concept

Example 1:

// file: demo_42.cs
//Author: SURESH BABU
//Purpose: Structure definition


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demo_42
{
    // Structure definition
    struct Fraction
    {
        public int numerator;
        public int denominator;

        public void print()
        {
            Console.WriteLine("  {0} / {1}", numerator, denominator);

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Fraction f;
            f.numerator = 5;
            f.denominator = 10;
            f.print();

            Fraction f2 = f;
            f2.print();

            //modify struct instance f2

            f2.numerator = 1;
            f.print();
            f2.print();
        }

    }
}


Example 2:
//File: demo_43
//Author: SURESH BABU
//Reference: Defining a structure to as reference class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demo_43
{
    struct Rectangle
    {
        private int m_width;
        public int width
        {
            get { return m_width; }
            set { m_width = value; }
        }

        private int m_height;
            public int height
            {
                get{return m_height;}
                set {m_height=value;}

            }

    }
    class Program
    {
        static void Main(string[] args)
        {
            int Area;
            Area = 0;
            Rectangle rect1 = new Rectangle();
            Console.WriteLine("Enter the value of widht and height of the Rectangle");
            rect1.width = int.Parse(Console.ReadLine());
            rect1.height = int.Parse(Console.ReadLine());
            Console.WriteLine("Dimensions of Width & Height");
            Console.WriteLine("____________________");
            Area = rect1.height * rect1.width;
            Console.WriteLine("Width:         {0}"+"cms"+"\nHeight:        {1}"+"cms", rect1.width, rect1.height);
            Console.WriteLine("\nArea of the Rectangle is {0}"+" sq.cms", Area);



        }
    }
}


An introduction to .NET

Microsoft Intermediate Language (IL)
                              Microsoft Intermediate Language is that it is not hidden in the depths of the machine.  IL is a full-fledged, stack-based language a bit like assembly code that can be written by hand if you’re feeling adventurous. There are also tools that enable you to disassemble IL and view the contents of the system objects and your own code.


An Introduction to.NET Memory Management
                       A .NET fact that has a lot of people interested, worried or just plain dumbstruck is that .NET runtime memory management is a garbage-collected (GC) system. Old programmers in particular have nightmares about the days of Lisp, when waiting for the garbage collector was a painful experience because he only came on Tuesdays. C++ programmers have had memory management drummed in to them so hard that to relinquish control of those allocations and deletions is anathema to them
The .NET memory management system approaches the allocation of memory resources differently. A block of memory allocated in the garbage collected or managed heap maintains a record of all the objects that refer to it. Only when those references have been released is the object destroyed. This relieves the burden of memory management from the programmer. You no longer have to remember to delete memory; you just stop using it.  A class no longer has to keep track of reference counts. It knows when to delete itself. To reduce heap fragmentation, the GC also moves objects to consolidate the used and free spaces in the managed store.


Finally its automated memory management – managed data to store when it is required and deletes the data or objects when it is not necessary. Since process is automatic, programmer need not to write separate code to manage the memory



. It prevents memory leaks and improves performance of heavily stressed serer systems. The managed heap also ensures that unsafe accesses, like buffer overflows and crashes, cannot modify the dta associated with other programs running on the same system. This makes the whole operating system more reliable and secure. Garbage collected systems have an unfair reputation for being slow and inefficient, but Microsoft has gone to considerable lengths to ensure that garbage collection in .NET really works. It’s very fast and does not impose a significant load on the CPU.


The .Net Framework Type System
There are two types of data types in .NET framework
·         Value types
·         Reference types

Value Types: These are including ints, chars, double, uint etc
Reference types: These are arrays, interfaces, classes and a native string type.


The .NET Framework System Objects
The centralized working part of the .NET Framework contains within a set of DLL’s that holds the system object model. The system namespace holds class hierarchies for collections, security, file I/O, graphics, Win32 API access, XML serialization, and many other important functions. All of the .NET system is available from C#, VB, or any of the supported languages