Enums are the way of creating user-defined data types, it is usually used to provide names for integral constants which makes the contract better for maintenance and reading. Enums restrict the variable with one of a few predefined values, these values of the enumerated list are called enums. Options are represented with integer values starting from zero, a default value can also be given for the enum. By using enums it is possible to reduce the bugs in the code.
Syntax:
enum <enumerator_name> {
element 1, element 2,....,element n
}
Example: In the below example, the contract Types consist of an enumerator week_days, and functions are defined to set and get the value of a variable of the type enumerator.
Solidity
pragma solidity ^0.5.0;
contract Types {
enum week_days
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
week_days week;
week_days choice;
week_days constant default_value
= week_days.Sunday;
function set_value() public {
choice = week_days.Thursday;
}
function get_choice(
) public view returns (week_days) {
return choice;
}
function getdefaultvalue(
) public pure returns(week_days) {
return default_value;
}
}
|
Output :

Struct
Structs in Solidity allows you to create more complicated data types that have multiple properties. You can define your own type by creating a struct.
They are useful for grouping together related data.
Structs can be declared outside of a contract and imported in another contract. Generally, it is used to represent a record. To define a structure struct keyword is used, which creates a new data type.
Syntax:
struct <structure_name> {
<data type> variable_1;
<data type> variable_2;
}
For accessing any element of the structure, ‘dot operator’ is used, which separates the struct variable and the element we wish to access. To define the variable of structure data type structure name is used.
Example: In the below example, the contract Test consists of a structure Book, and functions are defined to set and get values of the elements of the structure.
Solidity
pragma solidity ^0.5.0;
contract test {
struct Book {
string name;
string writter;
uint id;
bool available;
}
Book book1;
Book book2
= Book( "Building Ethereum DApps" ,
"Roberto Infante " ,
2, false );
function set_book_detail() public {
book1 = Book( "Introducing Ethereum and Solidity" ,
"Chris Dannen" ,
1, true );
}
function book_info(
) public view returns (
string memory, string memory, uint, bool ) {
return (book2.name, book2.writter,
book2.id, book2.available);
}
function get_details(
) public view returns (string memory, uint) {
return (book1.name, book1.id);
}
}
|
Output :

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
08 Jun, 2022
Like Article
Save Article