8 out of 12 rated this helpful - Rate this topic

struct (C# Reference)

A struct type is a value type that is typically used to encapsulate small groups of related variables, such as the coordinates of a rectangle or the characteristics of an item in an inventory. The following example shows a simple struct declaration:

public struct Book
{
    public decimal price;
    public string title;
    public string author;
}

Structs can also contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types, although if several such members are required, you should consider making your type a class instead.

Structs can implement an interface but they cannot inherit from another struct. For that reason, struct members cannot be declared as protected.

For more information, see Structs (C# Programming Guide).

For more information, see the following sections in the C# Language Specification:

  • 11 Structs

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
???
I realy don't get "struct", is to saying its for holding vars, what is it for?


CS Team: You probably would get a faster response to you question by posting it in the forums. You can find the C# Language Forum here: http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/threads .

[tfl - 10 10 10] Hi - and thanks for your post.You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn. You are much more likely get a quick response 

using the forums than through the Community Content.

For specific help about:
.Net Development : http://social.msdn.microsoft.com/Forums/en-US/category/visualstudio
Open Specifications : http://social.msdn.microsoft.com/Forums/en-US/category/openspecifications
SharePoint 2010 : http://social.msdn.microsoft.com/Forums/en-US/category/sharepoint2010
SQL : http://social.msdn.microsoft.com/Forums/en-US/category/sqlserver
Visual Studio : http://social.msdn.microsoft.com/Forums/en-US/category/visualstudio

A little about C# struct
A struct is meant to hold data of different types in a single data block.

So if you want to define a single datatype that holds all the data of a driver's license (name (string), DOB (date), height (double), personal photo (image)...etc) then one efficient way of doing it would be defining a struct with all the data pieces in it and accessing it as a single data block with different variables of different data types defined inside it.

Recommendation/Suggestion On Refinement To Struct
  1. a base type for structures such as "struct"
  2. more features available to struct when passed as generics, in particular, Parse and TryParse seems available on all structures but is available to the generic parameter instance.

  • 1/18/2008
  • G1