Click here to Skip to main content
15,887,935 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Why do we use structure and when to use structure in c#?
Thanks in Advance for your posts.
Posted

A very, very simple Google search woudl have found you this info a lot quicker than asking here - it's not as if it's a new question.
Have a look at this: http://programmers.stackexchange.com/questions/92339/when-do-you-use-a-struct-instead-of-a-class[^] which covers it pretty well.

In future, please try to do at least basic research yourself, and not waste your time or ours.
 
Share this answer
 
Structure:

A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. The declaration of a struct takes the following form:

Syntax:

[attributes] [modifiers] struct identifier [:interfaces] body [;]



where:

attributes (Optional)
Additional declarative information. For more information on attributes and attribute classes, see 17. Attributes.
modifiers (Optional)
The allowed modifiers are new and the four access modifiers.
identifier
The struct name.
interfaces (Optional)
A list that contains the interfaces implemented by the struct, all separated by commas.
body
The struct body that contains member declarations.

Example:
// keyword_struct.cs
// struct declaration and initialization
using System;
public struct Point 
{
   public int x, y;

   public Point(int p1, int p2) 
   {
      x = p1;
      y = p2;    
   }
}

class MainClass 
{
   public static void Main()  
   {
      // Initialize:   
      Point myPoint = new Point();
      Point yourPoint = new Point(10,10);

      // Display results:
      Console.Write("My Point:   ");
      Console.WriteLine("x = {0}, y = {1}", myPoint.x, myPoint.y);
      Console.Write("Your Point: ");
      Console.WriteLine("x = {0}, y = {1}", yourPoint.x, yourPoint.y);
   }
}


Output:

My Point:   x = 0, y = 0
Your Point: x = 10, y = 10
 
Share this answer
 
Comments
fjdiewornncalwe 6-Dec-12 14:10pm    
Plagiarism is not acceptable here at CP. If you are going to copy/paste something from another source, provide a link to the original source as reference. In this case: Source

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900