Dart Set length
Syntax & Examples


Set.length property

The `length` property in Dart returns the number of elements in a Set.


Syntax of Set.length

The syntax of Set.length property is:

 int length 

This length property of Set returns the number of elements in this.

Return Type

Set.length returns value of type int.



✐ Examples

1 Set of Numbers

In this example,

  1. We create a Set numbers containing some numbers.
  2. We retrieve the length using the length property.
  3. We print the length to standard output.

Dart Program

void main() {
  Set<int> numbers = {1, 2, 3};
  int length = numbers.length;
  print('Length of the set: $length');
}

Output

Length of the set: 3

2 Set of Characters

In this example,

  1. We create a Set characters containing some characters.
  2. We retrieve the length using the length property.
  3. We print the length to standard output.

Dart Program

void main() {
  Set<String> characters = {'a', 'e', 'i', 'o', 'u'};
  int length = characters.length;
  print('Length of the set: $length');
}

Output

Length of the set: 5

3 Set of Strings

In this example,

  1. We create a Set strings containing some strings.
  2. We retrieve the length using the length property.
  3. We print the length to standard output.

Dart Program

void main() {
  Set<String> strings = {'apple', 'banana', 'orange', 'cherry'};
  int length = strings.length;
  print('Length of the set: $length');
}

Output

Length of the set: 4

Summary

In this Dart tutorial, we learned about length property of Set: the syntax and few working examples with output and detailed explanation for each example.