C# Is Operator C# has an interesting operator know as is. The is operator is used for execution-time type checking. It is a binary operator that returns a Boolean



Download 33,56 Kb.
bet3/8
Sana11.01.2022
Hajmi33,56 Kb.
#347199
1   2   3   4   5   6   7   8
Bog'liq
si, as operatori OLMOSBEK

Is Vs As Operator Keyword In C#

The is operator is used to check if a type of an object is compatible with the given type at the run-time,



1 if(obj is SomeClass)

2 {

3 ...

4 }

whereas, as operator is used to perform casting between compatible reference types or Nullable types.



1 SomeClass someObject = obj as SomeClass;

With the “classic” method of type casting,



1 SomeClass someObject = (SomeClass) obj;

if the cast fails, an exception is thrown. However, with the as method, it results in null, which can be checked for, and avoid an exception being thrown.



1 SomeClass someObject = (obj as SomeClass) ?? new SomeClass();

Note: you can only use as with reference types, so if you are typecasting to a value type, you must still use the “classic” method.

Further Reading

  • C# Variable Types - C# is a type-safe language. Variables are declared as being of a particular type, and each variable is constrained to hold only values of its declared type. Variables can hold either value types or reference types, or they can be pointers. This lesson covers the value types and reference types.

  • Generics In C# - This article explains the Generics in C# with the help of a real-time problem and its step by step solution using c# generics. This article serves to be an in-depth post on getting started with generics in c#.

  • Writing Implicit and Explicit C# Conversion Operators by Jason Roberts - When writing custom classes we can provide behavior to allow for both explicit and implicit conversions to other types. Implicit conversion operators are those that don’t require an explicit cast. Explicit conversion operators are those that do require an explicit cast. In this article, Jason explains how to write them.

  • Nullable Types And Null Coalescing Operator C# - A reference type can have an actual value of null, meaning it has no value. A value type can’t have a value of null. This is why Nullables were added to the .Net Framework. This C# article discusses the Nullables with Null Coalescing operator and also explains the unique ways to use the Null Coalescing operator.


Download 33,56 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©www.hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish