0Pricing
Blockchain Smart Contracts with Solidity · درس

أنواع البيانات والمتغيرات في Solidity

تعلّم أنواع القيم وأنواع المراجع وكيفية تعريف المتغيرات وتهيئتها في Solidity.

أنواع البيانات والمتغيرات في Solidity درس مجاني في Blockchain Smart Contracts with Solidity على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Blockchain Smart Contracts with Solidity، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Blockchain Smart Contracts with Solidity 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Data Types: The Basics

Welcome to Solidity! Just like in any programming language, understanding data types is fundamental.

Data types tell Solidity what kind of information a variable will hold. This helps the Ethereum Virtual Machine (EVM) efficiently manage memory and prevent common errors.

Value Types Explained

Value types are the simplest data types. When you assign a value type variable or pass it to a function, a copy of its data is made.

Think of it like giving someone a photo – they get their own copy, and changes to their copy don't affect yours. They are stored directly in the variable's location.

Booleans and Integers

Let's start with common value types:

  • bool: Holds true or false.
  • uint: Unsigned integers (non-negative numbers). uint256 or simply uint is the default.
  • int: Signed integers (positive or negative numbers). int256 or int is the default.

Try running this simple contract to see them in action:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyNumbers {
  bool public isActive = true;
  uint public quantity = 100;
  int public temperature = -5;

  function getStatus() public view returns (bool) {
    return isActive;
  }

  function getQuantity() public view returns (uint) {
    return quantity;
  }

  function getTemperature() public view returns (int) {
    return temperature;
  }
}

The Address Type

The address type is crucial in Solidity. It's a 20-byte value representing an Ethereum account or a contract on the blockchain.

  • address: Can hold any Ethereum address.
  • address payable: Can also hold an Ethereum address, but specifically one that can receive Ether.

Here's how you might store a contract owner's address:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleWallet {
  address public owner;

  constructor() {
    owner = msg.sender; // The address that deployed this contract
  }

  function getOwner() public view returns (address) {
    return owner;
  }
}

Fixed-Size Byte Arrays

Solidity also has fixed-size byte arrays: bytes1 up to bytes32.

These are useful for storing small, fixed-length sequences of raw bytes, like cryptographic hashes or short identifiers. They are more efficient than dynamic arrays for known sizes.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract DataStore {
  bytes32 public documentHash;

  constructor() {
    // Example: A hash of some document content
    documentHash = 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef;
  }

  function getDocumentHash() public view returns (bytes32) {
    return documentHash;
  }
}

Reference Types Explained

Unlike value types, reference types don't store the data directly. Instead, they store a reference (a memory address) to where the data is located.

This means if you assign a reference type variable, both variables point to the same data. Changes made through one variable will be visible through the other.

Strings for Text

The string type is used for text data. It's a dynamic array of bytes that stores UTF-8 encoded characters.

Because its size can change, string is considered a reference type. You'll often see memory or calldata used with strings in function parameters or return types.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Greeter {
  string public currentMessage = "Hello, CoddyKit!";

  function setMessage(string memory _newMessage) public {
    currentMessage = _newMessage;
  }

  function getMessage() public view returns (string memory) {
    return currentMessage;
  }
}

Dynamic Bytes and Arrays

Solidity offers dynamic data structures:

  • bytes: A dynamic-sized byte array. Similar to string but for raw byte data.
  • Arrays (T[] or T[k]): Can hold a sequence of elements of the same type T. They can be fixed-size (T[k], where k is the length) or dynamic (T[]).
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract DataCollection {
  bytes public rawData = hex"1a2b3c"; // Dynamic bytes
  uint[] public dynamicNumbers; // Dynamic array of uints
  bool[2] public fixedBooleans = [true, false]; // Fixed-size array

  constructor() {
    dynamicNumbers.push(10);
    dynamicNumbers.push(20);
  }

  function getRawData() public view returns (bytes memory) {
    return rawData;
  }

  function getDynamicNumbersCount() public view returns (uint) {
    return dynamicNumbers.length;
  }
}

Custom Data with Structs

Sometimes, you need to group related data together. That's where structs come in handy!

A struct allows you to define your own custom data type by combining several variables of different types. Think of it like creating a blueprint for a record.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract UserProfile {
  struct User {
    uint id;
    address userAddress;
    string name;
    bool isActive;
  }

  User public adminUser;

  constructor() {
    adminUser = User(1, msg.sender, "Alice", true);
  }

  function getAdminName() public view returns (string memory) {
    return adminUser.name;
  }
}

Declaring & Initializing

Declaring a variable is straightforward: DataType visibility variableName;

If you don't explicitly initialize a variable, Solidity assigns a default value:

  • bool: false
  • uint/int: 0
  • address: 0x00...00 (zero address)
  • string/bytes/arrays: Empty value

It's good practice to initialize variables explicitly when they're declared.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract VariableLife {
  // Declared without explicit initialization (gets default values)
  uint public defaultBalance;
  bool public defaultStatus;

  // Declared and initialized
  uint public initialBalance = 1000;
  string public greeting = "Hello";

  function getDefaults() public view returns (uint, bool) {
    return (defaultBalance, defaultStatus);
  }
}

Data Type Challenge

Test your knowledge on Solidity data types!

Lesson Recap

Great job! In this lesson, you've learned about the fundamental data types in Solidity:

  • Value types (e.g., bool, uint, address, fixed bytesX) store data directly and are copied on assignment.
  • Reference types (e.g., string, dynamic bytes, arrays, structs) store a reference to data, allowing multiple variables to point to the same information.
  • You also learned how to declare and initialize variables, understanding their default values.

Next, we'll dive into how to control the flow of your smart contracts!

الأسئلة الشائعة

هل درس «أنواع البيانات والمتغيرات في Solidity» مجاني؟

نعم — نص درس «أنواع البيانات والمتغيرات في Solidity» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Blockchain Smart Contracts with Solidity، انتقل إلى CoddyKit PRO. تتضمن دورة Blockchain Smart Contracts with Solidity 4 دروس في المجموع.

ماذا ستتعلم في «أنواع البيانات والمتغيرات في Solidity»؟

تعلّم أنواع القيم وأنواع المراجع وكيفية تعريف المتغيرات وتهيئتها في Solidity. تتمرن على Blockchain Smart Contracts with Solidity مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Blockchain Smart Contracts with Solidity؟

لا تُشترط خبرة سابقة. Blockchain Smart Contracts with Solidity على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «أنواع البيانات والمتغيرات في Solidity»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Blockchain Smart Contracts with Solidity هذا؟

نعم. كل درس في Blockchain Smart Contracts with Solidity يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. أنواع البيانات والمتغيرات في Solidity
  2. بُنى التحكم والحلقات
  3. الدوال ومعدّلات الرؤية
  4. الهياكل والتعدادات
← العودة إلى Blockchain Smart Contracts with Solidity