0
0
C Sharp (C#)programming~10 mins

Casting with as and is operators in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Casting with as and is operators
Start with object
Check type with 'is'
YesSafe to cast
Use 'as' to cast
Cannot cast
Cast failed
Cast succeeded
First, check if an object is a certain type using 'is'. If yes, cast safely. Alternatively, use 'as' to cast and check for null to see if cast succeeded.
Execution Sample
C Sharp (C#)
object obj = "hello";
if (obj is string s) {
  Console.WriteLine(s.Length);
}
string? str = obj as string;
Console.WriteLine(str?.Length);
This code checks if obj is a string using 'is' and then casts with 'as', printing the string length both times.
Execution Table
StepExpressionEvaluationResultAction
1obj = "hello"Assign string to objobj = "hello"Store string in obj
2obj is string sCheck if obj is stringTrueCast obj to s
3Console.WriteLine(s.Length)s = "hello"5Print length 5
4str = obj as stringTry cast obj to string"hello"Assign to str
5Console.WriteLine(str?.Length)str = "hello"5Print length 5
6EndNo more code-Stop execution
💡 Reached end of code after printing string lengths
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
objnull"hello""hello""hello""hello"
snullnull"hello""hello""hello"
strnullnullnull"hello""hello"
Key Moments - 2 Insights
Why do we check 'obj is string s' before using s?
Because 'is' checks the type and safely casts only if obj is a string, preventing errors. See step 2 and 3 in execution_table.
What happens if 'obj as string' fails?
It returns null instead of throwing an error. We check for null before using the result. See step 4 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 's' after step 2?
A"hello"
Bnull
C5
Dobj
💡 Hint
Check the 'Result' column for step 2 in execution_table
At which step does the 'as' operator cast happen?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look for 'as' in the 'Expression' column in execution_table
If obj was not a string, what would 'str' be after step 4?
A0
Bnull
C"hello"
DException thrown
💡 Hint
Recall that 'as' returns null if cast fails, see key_moments explanation
Concept Snapshot
Casting with 'is' checks type and casts safely:
if (obj is Type t) { use t }

Casting with 'as' tries cast and returns null if fails:
Type? t = obj as Type;
Check for null before use.

Use 'is' to avoid exceptions and 'as' for nullable casts.
Full Transcript
This example shows how to use 'is' and 'as' operators in C# for casting. First, an object 'obj' holds a string "hello". Using 'is', we check if obj is a string and assign it to 's' if true. Then we print the length of 's'. Next, we use 'as' to cast obj to string and assign to 'str'. We print the length of 'str' using null-safe access. The execution table traces each step, showing variable values and actions. Key moments explain why 'is' is used to safely cast and how 'as' returns null if casting fails. The visual quiz tests understanding of variable values and casting steps. The snapshot summarizes the syntax and behavior of 'is' and 'as' casting in C#.