Home > DeveloperSection > Forums > how to use structure in c#
Ankit Singh

Total Post:341

Posted on    December-07-2015 9:39 PM

 C# C# 

 1 Reply(s)
 289  View(s)
Rate this:
I want to use Structure in c# please Help me.

aditya kumar Patel

Total Post:254

Posted on    December-07-2015 9:42 PM

You have already used a simple structure named Books. Structures in C# are quite different from that in traditional C or C++. The C# structures have the following features:

Structures can have methods, fields, indexers, properties, operator methods, and events.

Structures can have defined constructors, but not destructors. However, you cannot define a default constructor for a structure. The default constructor is automatically defined and cannot be changed.

Unlike classes, structures cannot inherit other structures or classes.

Structures cannot be used as a base for other structures or classes.

A structure can implement one or more interfaces.

Structure members cannot be specified as abstract, virtual, or protected.

When you create a struct object using the New operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the New operator.

If the New operator is not used, the fields remain unassigned and the object cannot be used until all the fields are initialized.

using System;
struct Books
    public string Title;
    public string AuthorName;
    public string Subject;
    public int BookID;

public class Structure
    public static void Main(string[] args)

        Books Book1;  
        Books Book2;  
        Book1.Title = "C Programming";
        Book1.AuthorName = "Raju Ali";
        Book1.Subject = "C Programming Tutorial";
        Book1.BookID = 145889501;

        Book2.Title = "Java Programming";
        Book2.AuthorName = "Aditya singh";
        Book2.Subject = "Java Programming Tutorial";
        Book2.BookID = 145879001;

        Console.WriteLine("Book 1 title : {0}", Book1.Title);
        Console.WriteLine("Book 1 author : {0}", Book1.AuthorName);
        Console.WriteLine("Book 1 subject : {0}", Book1.Subject);
        Console.WriteLine("Book 1 book_id :{0}", Book1.BookID);


        Console.WriteLine("Book 2 title : {0}", Book2.Title);
        Console.WriteLine("Book 2 author : {0}", Book2.AuthorName);
        Console.WriteLine("Book 2 subject : {0}", Book2.Subject);
        Console.WriteLine("Book 2 book_id : {0}", Book2.BookID);



Don't want to miss updates? Please click the below button!

Follow MindStick