Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 185635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:30:18+00:00 2026-05-11T15:30:18+00:00

I’m working through Josh Smith’s CommandSink code obviously do not understand something about the

  • 0

I’m working through Josh Smith’s CommandSink code obviously do not understand something about the ‘as’ keyword in C#.

I don’t understand why he wrote the line:

IsValid = _fe != null || _fce != null; 

since he only needed to write:

IsValid = depObj != null; 

Since it would never be the case the _fe would be null and _fce not null, or visa versa, right? Or am I missing something about how ‘as’ casts variables?

class CommonElement {     readonly FrameworkElement _fe;     readonly FrameworkContentElement _fce;      public readonly bool IsValid;      public CommonElement(DependencyObject depObj)     {         _fe = depObj as FrameworkElement;         _fce = depObj as FrameworkContentElement;          IsValid = _fe != null || _fce != null;     }     ... 

ANSWER:

The answer is what Marc said in his comment ‘that is the whole point of ‘as’ – it won’t throw an exception – it just reports null.’

and here is the proof:

using System;  namespace TestAs234 {     class Program     {         static void Main(string[] args)         {             Customer customer = new Customer();             Employee employee = new Employee();              Person.Test(customer);             Person.Test(employee);             Console.ReadLine();         }      }      class Person     {         public static void Test(object obj)         {             Person person = obj as Customer;              if (person == null)             {                 Console.WriteLine('person is null');             }             else             {                 Console.WriteLine('person is of type {0}', obj.GetType());             }         }     }      class Customer : Person     {         public string FirstName { get; set; }         public string LastName { get; set; }     }      class Employee : Person     {         public string FirstName { get; set; }         public string LastName { get; set; }     }  } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:30:19+00:00Added an answer on May 11, 2026 at 3:30 pm

    as will return an object of the type you requested, if the operand is compatible. If it isn’t, it will return null. If you use as and it is possible that the cast will fail, you need to check to make sure the reference is valid.

    For example, if depObj was of type String, it would not be null, but it would also not be able to be converted to either of the requested types and both of those variables would become null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 145k
  • Answers 145k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think sending the EM_SETTABSTOPS message to the TextBox will… May 12, 2026 at 8:50 am
  • Editorial Team
    Editorial Team added an answer For example: [[NSData alloc] initWithContentsOfFile:@"this/path/doesn't/exist/"]; [[NSImage alloc] initWithContentsOfFile:@"unsupportedFormat.sjt"]; [NSImage imageNamed:@"AnImageThatIsntInTheImageCache"];… May 12, 2026 at 8:50 am
  • Editorial Team
    Editorial Team added an answer depending on your schema, it may be easier to just… May 12, 2026 at 8:50 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.