encyclopegia.org

Safe navigation operator

In object-oriented programming, the safe navigation operator (also known as optional chaining operator, safe call operator, null-conditional operator, null-propagation operator) is a binary operator that returns null if its first argument is null; otherwise it performs a dereferencing operation as specified by the second argument (typically an object member access, array index, or lambda invocation).[src] It is used to avoid sequential explicit null checks and assignments and replace them with method/property chaining.[src] In programming languages where the navigation operator (e.g.[src] ".") leads to an error if applied to a null object, the safe navigation operator stops the evaluation of a method/field chain and returns null as the value of the chain expression.[src] It was first used by Groovy 1.0 in 2007 and is currently supported in languages such as C#, Swift, TypeScript, Ruby, Kotlin, Rust, JavaScript, and others.[src] There is currently no common naming convention for this operator, but safe navigation operator is the most widely used term.[src] The main advantage of using this operator is that it avoids the pyramid of doom.[src] Instead of writing multiple nested if s, programmers can just use usual chaining, but add question mark symbols before dots (or other characters used for chaining).…[src]

In object-oriented programming, the safe navigation operator (also known as optional chaining operator, safe call operator, null-conditional operator, null-propagation operator) is a binary operator that returns null if its first argument is null; otherwise it performs a dereferencing operation as specified by the second argument (typically an object member access, array index, or lambda invocation).[src] It is used to avoid sequential explicit null checks and assignments and replace them with method/property chaining.[src] In programming languages where the navigation operator (e.g.[src] ".") leads to an error if applied to a null object, the safe navigation operator stops the evaluation of a method/field chain and returns null as the value of the chain expression.[src] It was first used by Groovy 1.0 in 2007 and is currently supported in languages such as C#, Swift, TypeScript, Ruby, Kotlin, Rust, JavaScript, and others.[src] There is currently no common naming convention for this operator, but safe navigation operator is the most widely used term.[src] The main advantage of using this operator is that it avoids the pyramid of doom.[src] Instead of writing multiple nested if s, programmers can just use usual chaining, but add question mark symbols before dots (or other characters used for chaining).[src] While the safe navigation operator and null coalescing operator are both null-aware operators, they are operationally different.[src] ==Examples== Safe navigation operator examples in Apex: a[x]?.aMethod().aField // Evaluates to null if a[x] == null a[x].aMethod()?.aField // returns null if a[x].aMethod() evaluates to null String profileUrl = user.getProfileUrl()?.toExternalForm(); return [SELECT Name FROM Account WHERE Id = :accId]?.Name; ===C#=== C# 6.0 and above have ?.[src] , the null-conditional member access operator (which is also called the Elvis operator by Microsoft and is not to be confused with the general usage of the term Elvis operator, whose equivalent in C# is ??[src] , the null coalescing operator) and ?[] , the null-conditional element access operator, which performs a null-safe call of an indexer get accessor.[src] If the type of the result of the member access is a value type, the type of the result of a null-conditional access of that member is a nullable version of that value type.[src] The following example retrieves the name of the author of the first article in an array of articles (provided that each article has an Author member and that each author has a Name member), and results in null if the array is null , if its first element is null , if the Author member of that article is null , or if the Name member of that author is null .[src] Note that an System.IndexOutOfRangeException is still thrown if the array is non-null but empty (i.e.[src] zero-length).[src] string name = articles?[0]?.Author?.Name; Calling a lambda requires callback?.Invoke() , as there is no null-conditional invocation ( callback?() is not allowed).[src] using System; Func callback = x => x * x; // squares x int result = callback?.Invoke(5); (some-> article .author .name) ===CoffeeScript=== Existential operator: zip = lottery.drawWinner?().address?.zipcode ===Crystal=== Crystal supports the try safe navigation method name = article.try &.author.try &.name Conditional member access operator: var name = article?.author?.name ===Gosu=== Null safe invocation operator: var name = article?.author?.name The null-safe invocation operator is not needed for class attributes declared as Gosu Properties: class Foo { var _bar: String as Bar } var foo: Foo = null // the below will evaluate to null and not return a NullPointerException var bar = foo.Bar ===Groovy=== Safe navigation operator and safe index operator: def name = article?.authors?[0].name const name = article?.authors?.[0]?.name; const result = callback?.(); It short-circuits the whole chain of calls on its right-hand side: in the following example, bar is not "accessed".[src] const b = null?.foo.bar; Optional chaining operator was included in the TypeScript 3.7 release: let x = foo?.bar?.[0]?.baz(); ===Kotlin=== Safe call operator: val name = article?.author?.name ===Objective-C=== Normal navigation syntax can be used in most cases without regarding NULLs, as the underlying messages, when sent to NULL, is discarded without any ill effects.[src] NSString *name = article.author[0].name; my $name = $article?->author?->name; $name = $article?->author?->name; ===Raku (Perl 6)=== Safe method call: my $name = $article.?author.?name; ===Ruby=== Ruby supports the &.[src] safe navigation operator (also known as the lonely operator) since version 2.3.0: name = article&.author&.name ===Rust=== Rust provides a ?[src] operator that can seem like a safe navigation operator.[src] However, a key difference is that when ?[src] encounters a None value, it doesn't evaluate to None .[src] Instead, it behaves like a return statement, causing the enclosing function or closure to immediately return None .[src] The Option methods map() and and_then() can be used for safe navigation, but this option is more verbose than a safe navigation operator: fn print_author(article: Option ) { println!( "Author: {}", article.and_then(|y| y.author) .map(|z| z.name) .unwrap_or("Unknown".to_owned()) ); } An implementation using ?[src] will print nothing (not even "Author:") if article is None or article.unwrap().author is None .[src] As soon as ?[src] sees a None , the function returns.[src] fn try_print_author(article: Option ) -> Option { println!("Author: {}", article?.author?.name); Some(()) } val name = article.?.author.?.name : @ ?[src] The @ ?[src] annotation can be used to denote a nullable value.[src] case class Tree(left: Tree @ ?[src] = null, right: Tree @ ?[src] = null, value: String @ ?[src] = null) val root: Tree @ ?[src] = Tree( left = Tree( left = Tree(value = "left-left"), right = Tree(value = "left-right") ), right = Tree(value = "right") ) The normal .[src] in Scala is not null-safe, when performing a method on a null value.[src] a[NullPointerException] should be thrownBy { root.right.left.right.value // root.right.left is null![src] } The exception can be avoided by using ?[src] operator on the nullable value instead: root.?.right.?.left.?.value should be(null) The entire expression is null if one of ?[src] is performed on a null value.[src] The boundary of a null safe operator ?[src] is the nearest enclosing expression whose type is annotated as @ ?[src] .[src] ("Hello " + ("world " + root.?.right.?.left.?.value)) should be("Hello world null") ("Hello " + (("world " + root.?.right.?.left.?.value.?): @ ?)) should be("Hello null") (("Hello " + ("world " + root.?.right.?.left.?.value.?)): @ ?) should be(null) ===Swift=== Optional chaining operator, subscript operator, and call: let name = article?.authors?[0].name let result = protocolVar?.optionalRequirement?() ===Visual Basic .NET=== Visual Basic 14 and above have the ?.[src] (the null-conditional member access operator) and ?() (the null-conditional index operator), similar to C#.[src] They have the same behavior as the equivalent operators in C#.[src] The following statement behaves identically to the C# example above.[src] Dim name = articles?(0)?.Author?.Name ==See also== Elvis operator Null coalescing operator ==References== ==External links== PEP 505, discussing the possibility of safe navigation operators for Python[src]

58 sentences · 58 cited inline · 0 references stored

References

No reference rows published for this article.

Cite this article

MLA

"Safe navigation operator." encyclopegia.org, 2026, https://encyclopegia.org/wiki/safe-navigation-operator. Accessed 27 November 2026.

Chicago

"Safe navigation operator." encyclopegia.org. Accessed November 27, 2026. https://encyclopegia.org/wiki/safe-navigation-operator.

APA

encyclopegia.org. (2026). Safe navigation operator. Retrieved November 27, 2026, from https://encyclopegia.org/wiki/safe-navigation-operator

Accessed November 27, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

tour operator

stub · references available

Safe & Sound

stub · references available

Prewitt operator

stub · references available

Hello Operator

stub · references available

fail-safe

stub · references available

radio navigation

stub · references available

Ocean Steam Navigation Company

stub · references available

Nelson Steam Navigation Company Ltd.

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1378374973 · 0.5 · snapshot enwiki-001378374973

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools
  • Careers — open roles: frontier engineers and sales partners

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.