Structures in dotnet and Struct v/s class in dotnet (using C#.net)
CONTENTS
You might want to track the following attributes about each book: The Title, Author, Subject, and Book ID
- C# Structure
- Structure v/s Class
Structure
It is also a user-defined type like a class or interface. A C# structure is as same as a class because in C# a structure can contain each and every member what a class can contain like variables method constructors etc. If you need a small data structure that can hold a data that you won't modify later, structs are the best. In C#, a structure is a value type. It helps you to make a single variable to hold related data of various data types. The struct keyword is used for creating a structure. Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book: The Title, Author, Subject, and Book ID
Defining a Structure
To define a structure, you must use the struct keyword. The struct keyword defines a new data type, with more than one member for your program. For example, here is the way you can declare the Book structure:
using System;
namespace DictPOC
{
//Declaration of structure
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
class structs
{
public static void Main(string[] args)
{
/*Declare Book1 and Book2 of type Book*/
Books Book1;
Books Book2;
/*book 1 specification */
Book1.title = "C Programming";
Book1.author = "Dennis Ritchie ";
Book1.subject = "C Programming Tutorial";
Book1.book_id = 6495407;
/* book 2 specification */
Book2.title = "Telecom Billing";
Book2.author = "Zara Ali";
Book2.subject = "Telecom Billing Tutorial";
Book2.book_id = 6495700;
/* print Book1 info */
Console.WriteLine("Book 1 Info:- ");
Console.WriteLine("Book 1 title : " + Book1.title);
Console.WriteLine("Book 1 author : " + Book1.author);
Console.WriteLine("Book 1 subject : " + Book1.subject);
Console.WriteLine("Book 1 book_id :" + Book1.book_id + "\n");
/* print Book2 info */
Console.WriteLine("Book 2 Info:- ");
Console.WriteLine("Book 2 title : " + Book2.title);
Console.WriteLine("Book 2 author : " + Book2.author);
Console.WriteLine("Book 2 subject : " + Book2.subject);
Console.WriteLine("Book 2 book_id : " + Book2.book_id);
Console.ReadLine();
}
}
}
namespace DictPOC
{
//Declaration of structure
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
class structs
{
public static void Main(string[] args)
{
/*Declare Book1 and Book2 of type Book*/
Books Book1;
Books Book2;
/*book 1 specification */
Book1.title = "C Programming";
Book1.author = "Dennis Ritchie ";
Book1.subject = "C Programming Tutorial";
Book1.book_id = 6495407;
/* book 2 specification */
Book2.title = "Telecom Billing";
Book2.author = "Zara Ali";
Book2.subject = "Telecom Billing Tutorial";
Book2.book_id = 6495700;
/* print Book1 info */
Console.WriteLine("Book 1 Info:- ");
Console.WriteLine("Book 1 title : " + Book1.title);
Console.WriteLine("Book 1 author : " + Book1.author);
Console.WriteLine("Book 1 subject : " + Book1.subject);
Console.WriteLine("Book 1 book_id :" + Book1.book_id + "\n");
/* print Book2 info */
Console.WriteLine("Book 2 Info:- ");
Console.WriteLine("Book 2 title : " + Book2.title);
Console.WriteLine("Book 2 author : " + Book2.author);
Console.WriteLine("Book 2 subject : " + Book2.subject);
Console.WriteLine("Book 2 book_id : " + Book2.book_id);
Console.ReadLine();
}
}
}
Features of C# Structures:
Structures in C# are quite different from that in traditional C or C++. The C# structures have the following features:
- Structures can have methods, fields, indexers, properties, and events.
- Structures can have defined constructors, but not destructors. However, you cannot define a default constructor for a structure.
- 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.
- Unlike classes, structs can be instantiated without using the new operator.
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 a default constructor
Define me dotnet
ReplyDeleteI'll soon create another post for Introduction of Dotnet..
DeleteDefine me dotnet
ReplyDeleteGood initiative Sir :)
ReplyDeleteNice article
Delete