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


Friday, July 19, 2013

Usage of Enum in C#


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

enum days
{
    monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

namespace demo_enum
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(days.Friday);
            Console.WriteLine((int)days.Sunday);
            Console.WriteLine((int)days.Tuesday);
        }
    }
}


Output produces likes....

Friday
6
1

C# Code to generate table froms 01 - 10

C# Code to generate table froms 01 - 10 using Microsoft Visual Studio
------------------------------------------------------------------------------------------------


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


namespace Table4{

class Program
{
static void Main(string[] args)
{

int prod = 0;
for (int row = 1; row <= 10; row++){

for (int col = 1; col <= 10; col++){
prod = row * col;

Console.Write(prod+"\t");}

Console.WriteLine();}

Console.WriteLine();}
}
}

.... You will get results like


Access modifiers in C#

Dot.Net Tutorials

aAccess modifiers
  • ·          Public                          Access not limited
  • ·         Private:                        Access limited to the containing of the class or a type
  • ·         Internal:                       Access limited to the containing of the program only
  • ·         Protected:                    Access limited to the containing class or types derived from the  
  •                                                                                                                    contained class
  • ·         Protected Internal:         Access limited to the program or types derived from the class


----------------------------------------------------------------------------------------------------
Example for public

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

namespace demo_18
{
   public  class test
    {
        public int x;
        public int y;
    }
    class Program
    {
        static void Main(string[] args)
        {
            int result;
            test test1 = new test();
            test1.x = 5;
            test1.y = 10;
            result = test1.x + test1.y;
            Console.WriteLine(result);

        }
    }
}

-- In the above program, class 'test' is initialized with public access modifier, hence that class is accessed by another class i.e., program

So we would get the output as

Output:

15
... press any key to continue



 Private :

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

namespace demo_18
{
   private  class test
    {
        public int x;
        public int y;
    }
    class Program
    {
        static void Main(string[] args)
        {
            int result;
            test test1 = new test();
            test1.x = 5;
            test1.y = 10;
            result = test1.x + test1.y;
            Console.WriteLine(result);

        }
    }
}

In  above namespace, demo_18 class test is intizlised with private access modifier, where the accessibility of the test class is limited to the class test only... So above program with produce error..

" Error 1 Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal D:\Suresh Babu\demo_18\demo_18\Program.cs 8 24 demo_18"


Saturday, February 9, 2013

Netsh:
Netsh is expanded as Network shell deployed in windows NT and higher systems begining with windows 2000 operating systems. It allows local or remote configuration as an interface.

A common use of Netsh is to reset TCP/IP stack to default.


Common Usage of Netsh Commonds are:


netsh interface ip reset C:\resetlog.txt                                              <<For resettting the log file>>
netsh interface ip set address name="Local Area Connection" static 192.168.1.105 255.255.255.0 192.168.1.1 <<For setting ip address manually>>
netsh interface ip set address name="Local Area Connection" dhcp <<To attain the IP Address through DHCP>>
netsh interface ip add address local 234.234.234.234 255.255.255.0 <<To add secondary ip (Dual IP)>>

Tuesday, February 5, 2013

Date() in Javascript

Display Date in a webpage using Javascript:

<!Doctype html>
<head>
<title>Basic_webpage</title>
<body>
<h1>Today Date</h1>
<br />
<script>
document.write(Date());
</script>
</body>
<head>
</html>

--------------------------------------------------------------

It will display results as 

Today Date


Tue Feb 05 2013 16:48:32 GMT+0530 (India Standard Time)