Part 9: C# Tutorial – Introduction to Struct



In this article we will cover following points

1. Struct definition
2. Struct members
3. Inheritance in struct
4. Struct instantiation, member initialization & accessibility

Struct definition

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 struct type is suitable for representing lightweight objects such as Point, Rectangle,
and Color.

Struct is declared using the keyword struct.

public struct
{
//members (fields, methods etc.)
}

Structs can contain
1. Constructors
2. Constants
3. Fields
4. Methods
5. Properties
6. Indexers
7. Operators
8. Events
9. nested types
if several such members are required, you should consider making your type a class
instead.

Inheritance in struct
1. A struct can not inherit from another struct or class.
2. A struct can not be base of a class.
3. Struct can implement interfaces, and it does that exactly as classes do..

Struct instantiation, member initialization & accessibility

1. It is an error to define a default (parameterless) constructor for a struct.
2. You can initialize struct members only by using a parameterized constructor.
3. structs can be instantiated without using the new operator.


3 thoughts on “Part 9: C# Tutorial – Introduction to Struct

  1. Santo Guilliam

    Hi There! We are searching for some people that are interested in from working their home on a part-time basis. If you want to earn $500 a day, and you don’t mind developing some short opinions up, this might be perfect opportunity for you! Simply click the link here NOW!

  2. Sabra Baraby

    Hi There! We are searching for experienced people that might be interested in from working their home on a part-time basis. If you want to earn $500 a day, and you don’t mind writing some short opinions up, this is the perfect opportunity for you! Simply click the link here NOW!

Leave a Reply

Your email address will not be published. Required fields are marked *