- C# Basics
- C# Home
- C# Program Structure
- C# Basic Syntax
- C# Data Types
- C# Variables
- C# Type Conversion
- C# Constants
- C# Operators
- C# Decision Making
- C# Loops
- C# Arrays
- C# Strings
- C# Methods
- C# Nullables
- C# Structure
- C# Enum
- C# Object Oriented
- C# Classes
- C# Encapsulation
- C# Inheritance
- C# Polymorphism
- C# Operator Overloading
- C# Advance
- C# Namespaces
- C# Interfaces
- C# Preprocessors
- C# Regular Expressions
- C# Exception Handling
- C# File I/O
- C# Test
- C# Online Test
- Give Online Test
- All Test List
C# Structure
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 »
Like/Share Us on Facebook 😋