Main Menu

HOME

.Net
ASP
Assembly
C
C++
Delphi
HTML
Java
JavaScript
MySQL
PC interface
Powershell
Perl
PHP
VBScript
Visual Basic
XML

Make money selling software. Check this out here

Hosted clickbank mall. Check this out here




   Misc

   Amazon

   Links

Structures in C#


 

Structures (keyword struct) are light-weight objects. They are mostly used when only a data container is required for a collection of value type variables. Structs are similar to classes in that they can have constructors, methods, and even implement interfaces, but there are important differences. Structs are value types while classes are reference types, which means they behave differently when passed into methods as parameters. Another very important difference is that structs cannot support inheritance. While structs may appear to be limited with their use, they require less memory and can be less expensive if used in the proper way.

A struct can, for example, be declared like this:

struct Person
{
    string name;
    System.DateTime birthDate;
    int heightInCm;
    int weightInKg;
}
                                          

The Person struct can then be used like this:

Person dana = new Person();
dana.name = "Dana Developer";
dana.birthDate = new DateTime(1974, 7, 18);
dana.heightInCm = 178;
dana.weightInKg = 50;

if (dana.birthDate < DateTime.Now)
{
    Console.WriteLine("Thank goodness! Dana Developer isn't from the future!");
}
                                          

It is also possible to provide constructors to structs to make it easier to initialize them:

using System;
struct Person
{
    string name;
    DateTime birthDate;
    int heightInCm;
    int weightInKg;

    public Person(string name, DateTime birthDate, int heightInCm, int weightInKg)
    {
        this.name = name;
        this.birthDate = birthDate;
        this.heightInCm = heightInCm;
        this.weightInKg = weightInKg;
    }
}

public class StructWikiBookSample
{
    public static void Main()
    {
        Person dana = new Person("Dana Developer", new DateTime(1974, 7, 18), 178, 50);
    }
}
                                          

 

All text is available under the terms of the GNU Free Documentation License
Source : Wikibooks


 

 



 




   Sponsors
 

   Software
500 Java Tips E-book
PHP editor
PERL editor
Beginning Java
Beginning Visual Basic
Learn VB.net
Learn VB 6
VB and databases
ASP image library
C++ builder programming
C++ fundamentals

   Source Code
Generate a random password(PHP)
date and time in status bar(Javascript)
create a text file(VBScript)
multi status bar messages(Javascript)
random lines(VB)
list service in a listbox(VB.net)
border styles(CSS)
display installed hotfixes(VBScript)




Copyright © 2004 by programmershelp.co.uk