AllInWorld99 provides a reference manual covering many aspects of web programming, including technologies such as HTML, XHTML, CSS, XML, JavaScript, PHP, ASP, SQL,FLASH, jQuery, java, for loop, switch case, if, if else, for...of, for...in, for...each,while loop, blogger tips, blogger meta tag generator, blogger tricks, blogger pagination, client side script, html code editor, javascript editor with instant output, css editor, online html editor, materialize css tutorial, materialize css dropdown list,break, continue statement, label,array, json, get day and month dropdown list using c# code, CSS button,protect cd or pendrive from virus, cordova, android example, html and css to make android app, html code play,telerik show hide column, Transparent image convertor, copy to clipboard using javascript without using any swf file, simple animation using css, SQL etc. AllInWorld99 presents thousands of code examples (accompanied with source code) which can be copied/downloaded independantly. By using the online editor provided,readers can edit the examples and execute the code experimentally.


     A structure allows you to create your own custom data types and it contains one or more
members that can be of different data types. It can contain fields, methods, etc.

    Structures are very similar to classes but there are some restrictions present in the case of
structures that are absent in the case of classes. For example you cannot initialize
structure members. Also you cannot inherit a structure whereas classes can be inherited.
Another important feature of structures differentiating it from classes is that a structure
can't have a default parameter-less constructor or a destructor. A structure is created on
the stack and dies when you reach the closing brace in C# or the End structure in
VB.NET.

   But one of the most important differences between structures and classes is that structures
are referenced by value and classes by reference. As a value type, allocated on the stack,
structs provide a significant opportunity to increase program efficiency. Objects on the
stack are faster to allocate and de-allocate. A struct is a good choice for data-bound
objects, which don’t require too much memory. The memory requirements should be
considered based on the fact that the size of memory available on the stack is limited than
the memory available on the heap.
Thus we must use classes in situations where large objects with lots of logic are required.

Struct – Code: Sample code showing the Class vs. Structures

Class-Example:-
using System;
class Test
{
    int classvar ;
    int anothervar =20;
    public Test ( )
    {
        classvar = 28;
    }
    public static void Main()
    {
        Test t = new Test();
        ExampleStruct strct = new ExampleStruct(20);
        System.Console.WriteLine(strct.i);
        strct.i = 10;
        System.Console.WriteLine(t.classvar);
        System.Console.WriteLine(strct.i);
        strct.trialMethod();
    }
}

Struct-Example:-

struct ExampleStruct
{
    public int i;
    public ExampleStruct(int j)
    {
        i = j;
    }
    public void trialMethod()
    {
        System.Console.WriteLine("Inside Trial Method");
    }
}

output
O/P:-
28
20
10
Inside Trial Method



Advertisement

0 comments:

Post a Comment

Total Pageviews