=>Structure is a value type data type.
=>It helps you to make single variable to hold related data in different data types.
Points to Remember:
Structures can have methods, fields, indexers, properties, operator methods, and events.
Structures can have defined constructors, but not destruction. However, you cannot define a default constructor for a structure. The default constructor is automatically defined and cannot be changed.
Unlike classes, structures cannot inherit other structures or classes.
Structures cannot be used as a base for other structures or classes.
A structure can implement one or more interfaces.
Structure members cannot be specified as abstract, virtual, or protected.
When you create a struct object using the New operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the New operator.
If the New operator is not used, the fields remain unassigned and the object cannot be used until all the fields are initialized.
*** If you want to initialize fields inside the struct it get compile error.
Example:
struct Books
{
public string Tittle;
public string BookName;
public int bookId;
}
class Program
{
static void Main(string[] args)
{
Books b1;
b1.Tittle = "XYZ";
b1.BookName = "Name";
b1.bookId = 1;
Books obj = new Books();
obj.Tittle = "Alok";
Console.WriteLine(b1.BookName);
Console.ReadLine();
}
}
=>It helps you to make single variable to hold related data in different data types.
Points to Remember:
Structures can have methods, fields, indexers, properties, operator methods, and events.
Structures can have defined constructors, but not destruction. However, you cannot define a default constructor for a structure. The default constructor is automatically defined and cannot be changed.
Unlike classes, structures cannot inherit other structures or classes.
Structures cannot be used as a base for other structures or classes.
A structure can implement one or more interfaces.
Structure members cannot be specified as abstract, virtual, or protected.
When you create a struct object using the New operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the New operator.
If the New operator is not used, the fields remain unassigned and the object cannot be used until all the fields are initialized.
*** If you want to initialize fields inside the struct it get compile error.
- struct MyStruct {
- int x = 20; // Error its not possible to initialize
- int y = 20; // Error its not possible to initialize
- }
Class versus Structure
- Classes and Structures have the following basic differences
- classes are reference types and structs are value types
- structures do not support inheritance
- structures cannot have default constructor
Example:
struct Books
{
public string Tittle;
public string BookName;
public int bookId;
}
class Program
{
static void Main(string[] args)
{
Books b1;
b1.Tittle = "XYZ";
b1.BookName = "Name";
b1.bookId = 1;
Books obj = new Books();
obj.Tittle = "Alok";
Console.WriteLine(b1.BookName);
Console.ReadLine();
}
}
It's good!
ReplyDelete