Learn Solidity by Examples
0.8.xSolidity is an object-oriented, high-level language for implementing smart contracts. It is designed to target the Ethereum Virtual Machine (EVM).
These examples cover the core concepts of Solidity development, from basic data structures and state variables to advanced features like inheritance, interfaces, and error handling.
State Variables
Understanding blockchain storage with this sample code demonstrating state variable declarations, public visibility modifiers, constructor initialization, and view functions for reading contract state without gas costs.
Data Types
Comprehensive guide to Solidity data types with this code example covering booleans, unsigned and signed integers, the special address type for Ethereum accounts, fixed-size bytes arrays, and enums for modeling finite state machines.
Mappings
Master Solidity mappings with this sample code showing hash table-like key-value storage, virtual initialization returning default values for unset keys, nested mapping structures, and the msg.sender global variable for caller identification.
Arrays
Working with Solidity arrays through this code example demonstrating dynamic and fixed-size array declarations, push and pop operations, the delete keyword behavior, length property access, and gas considerations for large datasets.
Structs
Organizing complex data with this sample code showing struct definitions for grouping related variables, array of structs for collections, storage vs memory data locations, and direct struct modification through storage pointers.
Modifiers
Implementing access control and validation with this code example covering function modifier definitions, the require statement for condition checking, the underscore merge point, and chaining multiple modifiers for comprehensive input validation.
Events
Blockchain logging and frontend communication with this sample code demonstrating event declarations, indexed parameters for efficient filtering, emit statements for writing logs, and how external applications listen for contract events using Web3.js.
Interfaces
Contract interaction patterns with this code example showing interface definitions for external contracts, the IERC20 token standard, casting addresses to interface types, and calling functions on deployed contracts without knowing their implementation.
Inheritance
Object-oriented programming in Solidity with this sample code covering contract inheritance using the is keyword, virtual functions allowing overrides, the override keyword for replacing parent implementations, and super for calling parent contract functions.
Error Handling
Robust error handling with this code example demonstrating custom error definitions for gas efficiency, the revert statement with error parameters, require for input validation, assert for invariant checking, and transaction rollback behavior.

