• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C# » Introduction » What is Structure in C#
Next →
← Prev

What is Structure in C#

By Dinesh Thakur

Structure is a user-defined value type which encapsulates member data and member function.

A struct is a user-defined value type. It is declared in a very similar way to a class, except that it can’t inherit from any class, nor can any class inherit from it (as mentioned\ previously, however, all value types do inherit from System.object), The following example shows a partial declaration for a ‘Coordinate’ struch.

struct Coordinate

{

pub1i c int x;

public int y;

public Coordinate(int x, int y)

{

this . x = x;

this.y = y;

}

}

 

Given the above, one could initialise a Coordinate type in a familiar way, using code like:

 

coordinate c = new coordinate(10, 2);

~ Concept

Note that if a variable of a struct type is declared without being given an explicit value, eg:

 

Coordinate c2 ;

 

it does not equate to ‘null’ (this being the default value for reference types, rather than value types. Instead, the variable is initialised to a state where its fields have their default values.If these fields are basic value types, they will generally be set to zero. If these field they will be set to ‘null’.

 

Because of this default initialization behaviour, it is an error for a struct to be given a parameterless constructor (eg. one like ‘public Coordinate().) Also, where a struct does have a constructor, you should be sure to make assignments to all of the struct’s fields within this constructor.

 

Use of structure

 

using system;

public struct Storeitem

{

public long ItemNumber;

public string ItemName;

public double unitprice;

 

public void DisplayAnltem()

{

console.writeLine(“Department store”);

console.writeLine(“Item #: {0}”, this.ltemNumber);

console.writeLine(“Description: {0}”, this.ItemName);

Console.writeLine(“unit price: {0:C}\n”, this.unitprice);

}

}

public class Exercise

{

static void Main()

{

Storeltem item = new Storeltem ();

 

item.ltemNumber = 348649;

item.ItemName = “Men 3-piece Jacket”;

item.unitprice = 275.95;

 

item.DisplayAnltem ();

}

}

 

OUTPUT:

 

Department Store

Item #: 348649

Description: Men 3-piece Jacket

unit price: 275.95

You’ll also like:

  1. Structure in C++ Language
  2. ATM Structure
  3. Structure of a C+ + Program
  4. How to Referencing a structure
  5. Structure Containing Arrays in C
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C# Tutorials

C# Tutorials

  • C# - .NET Languages Types
  • C# - Dot Net
  • C# - Dot Net Framework
  • C# - Inheritance Types
  • C# - Features
  • C# - CTS
  • C# - CLS
  • C# - CLR
  • C# - Console
  • C# - MSIL
  • C# - Base Class Library
  • C# - Web Forms Creation
  • C# - C# Vs C++
  • C# - Statements Types
  • C# - JIT
  • C# - CLI
  • C# - Controls Types
  • C# - String Types
  • C# - Execution Model

Other Links

  • C# - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW