Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Dart as, is, is! operatörleri
Gülsen Keskin
Gülsen Keskin

Posted on

     

Dart as, is, is! operatörleri

Type test operatörleri:

as, is, ve is! operatörleri, çalışma zamanında(runtime) nesnelerin türlerini kontrol etmek için kullanılır.

"is" operatörü, nesne belirtilen türe sahipse geriye 'true' döner.

Nesnenin T türünde olduğundan emin değilseniz, nesneyi kullanmadan önce türü kontrol etmek için is T'yi kullanın.

if (employee is Person) {  // Type check  employee.firstName = 'Bob';}
Enter fullscreen modeExit fullscreen mode

"is!" operatörü, nesne belirtilen türde değilse geriye true döner.

"as" operatörü, Dart dilinde bir nesnenin başka bir türde olduğunu belirtmek için kullanılır. Örneğin, bir "Object" tipindeki nesnenin gerçekte bir "String" olduğunu belirtmek için "as" operatörünü kullanabilirsiniz.

Not: Bir nesneyi belirli bir türe çevirmek için as işlecini ancak ve ancak nesnenin o türde olduğundan eminseniz kullanın. Örneğin:

Kullanımı şöyledir:

object as String
Enter fullscreen modeExit fullscreen mode

Bu, "object" değişkeninin gerçekte bir "String" olduğunu belirtir ve bu değişkeni bir "String" türünde kullanmanıza izin verir. Eğer "object" değişkeni gerçekte bir "String" türü değilse, bu işlemin sonucu "null" olacaktır.

"as" operatörünün bir diğer önemli kullanımı, bir sınıfın alt sınıfı veya eşdeğeri olduğunu belirtmek için "is" operatörüyle birlikte kullanılır. Örneğin:

if (object is String) {  object as String;}
Enter fullscreen modeExit fullscreen mode

Bu kod, "object" değişkeninin bir "String" türü olduğunu test eder ve eğer öyleyse, "object" değişkenini bir "String" türünde kullanmanıza izin verir.

Kaynaklar:
https://dart.dev/guides/language/language-tour#type-test-operators
https://chat.openai.com/chat

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
beratkutluca profile image
Berat Kutluca
  • Joined

Aldığım kursta as in nesne tipine parse yaptığı şeklinde anlatılmıştı ve uygulama yaparken hata almıştım. Sizin anlatımınız sonucu as in aslında kesin olarak türü bilinen tipi ideye belirtmek için kullanıldığını öğrenince taşlar yerine oturdu. Teşekkür ederim.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

soft. dev. at harmonycloud
  • Location
    Sakarya, Türkiye
  • Education
    Pamukkale University
  • Work
    harmonyerp
  • Joined

More fromGülsen Keskin

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp