Dart Syntax: Identifiers

Dart Syntax: Identifiers

  • Identifiers can include letters (both uppercase and lowercase), digits, and underscores.

  • They must start with a letter (a-z or A-Z), an underscore (_), or a dollar sign ($).

  • Dart is case-sensitive, so myVariable and MyVariable are considered different identifiers.

  • Identifiers cannot be reserved words or keywords used by Dart.

  • Camel Case Convention:

    • Dart conventionally uses camel case for identifiers. In camel case, the first word starts with a lowercase letter, and the subsequent words are capitalized. This is commonly used for variables and functions.

        dartCopy codevar myVariable;
        void calculateTotalAmount() {
          // code
        }
      
  • Pascal Case Convention:

    • For class names, Dart often follows the Pascal case convention. In Pascal case, the first letter of each word is capitalized.

        dartCopy codeclass Person {
          // class definition
        }
      
  • Underscores:

    • Dart allows identifiers to start with an underscore. This is often used to indicate that the identifier is private to its library.

        dartCopy codevar _secretVariable;
      
  • Dollar Sign:

    • Dart allows the use of the dollar sign in identifiers. This is often used in string interpolation and can be part of variable names.

        dartCopy codevar $price = 10;
        print($price); // 10
      
  • Reserved Words:

    • Certain words are reserved by Dart and cannot be used as identifiers. For example, int, double, if, else, and so on.

        dartCopy code// This is not allowed
        var int = 5;