Lesson 6.7

C# Structs Statements with Programming Example


Objective

What is struct statement in c#?
How to declare and use struct?
Programming Example

What is struct statement in c#?

A structure is value type data type which is defined by struct keyword. It is very useful terms in c# programming that allows you to group related variable at one place. By this way you can easily memorize variable name. It encapsulates small group of related variables.

How to initialize structure

You can initialize structure with struct keyword:

  public struct struct_name
  {
  variable_list;
  }

A structure is initialized beyond class and it is almost same as class in nature. A structure can also have methods, constructors, fields, indexers, properties, operator methods, and events. The difference between class and structure is structure doesn’t support inheritance so neither it can be base class nor neither be inherited.

Features of Structure

  1. Structure is almost same as class because it also contains methods, constructors, fields, indexers, properties, operator methods, and events.
  2. Structure cannot have Destructor.
  3. It can implement one or more interfaces.
  4. Member of structure cannot be Abstract, virtual or protected.
  5. Structure is value type while class is reference type.
  6. It doesn’t support Inheritance.

Let’s understand it with this scenario.
For example, you are writing a program in which you need to gather information of book details and author details. Struct allows you to use same variable name in different group.

Book Details

BookName:
Price:
AuthorName:


Author Details

AuthorName:
Age:
Phone:

Here you have noticed that we have used AuthorName variable in both group. Now, let's see all these in a complete programming section.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace structs_statement
{
    class Program
    {
        //Creating Struct
        public struct BookDetails
        {
            public string BookName, Price, AuthorName;
        }
        //Creating Struct
        public struct AuthorDetails
        {
            public string AuthorName, Age, Mobile;
        }
        static void Main(string[] args)
        {
            BookDetails b = new BookDetails();
            b.BookName = "Advanced C#";
            b.Price = "$32";
            b.AuthorName = "Jack Richerd";

            AuthorDetails a = new AuthorDetails();
            a.AuthorName = "Jack Richerd";
            a.Age = "35 years";
            a.Mobile = "00892349934";

            Console.WriteLine("******Book Details*********");
            Console.WriteLine("Book Name : " + b.BookName);
            Console.WriteLine("Author Name : " + b.AuthorName);
            Console.WriteLine("Price : " + b.Price);

            Console.WriteLine("\n");

            Console.WriteLine("******Author Details*********");
            Console.WriteLine("Author Name : " + a.AuthorName);
            Console.WriteLine("Author Age : " + a.Age);
            Console.WriteLine("Mobile : " + a.Mobile);

            Console.ReadKey();


            
        }
    }
}


Output

******Book Details*********
Book Name : Advanced C#
Author Name : Jack Richerd
Price : $32

******Author Details*********
Author Name : Jack Richerd
Author Age : 35 years
Mobile : 00892349934
__

In the preceding example we have created 2 structure BookDetails and AuthorDetails and assign 3 public string variables in each struct. In the Main method we have created object for struct and assign value and printed on console. You can create as many object of struct as you want and use them for storing different values.

Summary

Struct statement is very useful in c# programming which makes your code clean and more readable. The next chapter explained Classes and Methods in c#.

BackNext