codescracker


c#

C# Structure



« Previous Tutorial Next Tutorial »


Structure is used to make a single variable hold related data of various data types. To create a structure in C#, use struct keyword

Defining a Structure in C#

Here is an example, showing how to define a structure in C# programming:

struct Books
{
   public string title;
   public string author;
   public string subject;
   public int book_id;
};  

C# Structure Example

Here is an example program, demonstrating the concept/use of structure in C#:

/* C# Structure - Example Program */
		
using System;
struct BOOKS
{
	public string title;
	public string author;
	public string subject;
	public int book_id;
};  
public class StructureClass
{
	public static void Main(string[] args)
	{
		BOOKS b1; 
		BOOKS b2; 
		
		b1.title = "C# Programming";
		b1.author = "codescracker.com"; 
		b1.subject = "C# Programming Tutorial";
		b1.book_id = 6495407;
	
		b2.title = "C# Programming Examples";
		b2.author = "codescracker.com";
		b2.subject =  "C# Programs Tutorial";
		b2.book_id = 6495700;
		
		Console.WriteLine("Book 1\nTitle = {0}", b1.title);
		Console.WriteLine("Author = {0}", b1.author);
		Console.WriteLine("Subject = {0}", b1.subject);
		Console.WriteLine("Book ID = {0}", b1.book_id);
	
		Console.WriteLine("\nBook 2\nTitle = {0}", b2.title);
		Console.WriteLine("Author = {0}", b2.author);
		Console.WriteLine("Subject = {0}", b2.subject);
		Console.WriteLine("Book ID = {0}", b2.book_id);       
	
		Console.ReadKey();
	
	}
}

When we compile and execute the above program, it will produce the following output:

Book 1
Title = C# Programming
Author = codescracker.com
Subject = C# Programming Tutorial
Book ID = 6495407

Book 2
Title = C# Programming Examples
Author = codescracker.com
Subject = C# Programs Tutorial
Book ID = 6495700

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test