C Sharp/Structs

From Meshplex

Jump to: navigation, search
Image:Csharp_programming.gif

Main Home

Basics
C# Tutorial Home
C# - Introduction to Visual Studio IDE
Introduction to C#
C# - Overview
C# - Statements
C# - Data Types
C# - Variables
C# - Operators
C# - Flow Control
C# - Variables II

C# - Functions and Methods
C# - Classes and Objects I
C# - Enumerations
C# - Dates and Times
C# - Random Numbers

Advanced
C# - Inheritance
C# - Polymorphism
C# - Garbage Collection
C# - Operator Overloading
C# - Encapsulation
C# - Properties
C# - Indexers
C# - Exceptions
C# - GUI
C# - Delegates
C# - Events
C# - Components
C# - Multithreading
C# - Regular Expressions
C# - Graphics and Multimedia
C# - Files and Streams
C# - XML
C# - Database, SQL and ADO.NET
C# - ASP.NET Web Forms and Web Controls
C# - Web Services
C# - Network Programming
C# - Datastructures and Collections
C# - Enumerations and Iterators
C# - .NET Assemblies
C# - CLR
C# - Visual Studio Debugger
C# - Namespaces
C# - Generics
C# - MS Intermediate Language
C# - Deploying Windows Application

[edit] Define a struct in csharp

Structs are nothing but a custom variable. Structs are composed of several pieces that make the struct up. Suppose that you want to create a struct that defines a customer. These are the pieces that will compose of the struct: Name, age, sex, birthday, join date, etc. All of this information can be placed in the struct.


struct <StructName>
{
     <declare members here>
}


Example:

using System;
 
class Program
{
    struct customer
    {
        public string name;
        public int age;
        public char sex;
    }
 
    static void Main(string[] args)
    {
        customer x;
        x.name = "john";     
        x.age = 18;
        x.sex = 'm';
 
        Console.WriteLine("Customer Info");
        Console.WriteLine("Name:  " + x.name);
        Console.WriteLine("Age:  " + x.age);
        Console.WriteLine("Sex:  " + x.sex);
        Console.Read();
    }
}


Output:

Customer Info
Name:  john
Age:  18
Sex:  m


  • public keyword is used so you can access the variables from the main block. Always use keyword public
  • You can also declare multiple structs in the same program
  • Use the . operator to access and assign parts of the struct
Personal tools
Interesting Sites
ListSergeant