<p>What is actually the difference between these two casts?</p>
<pre><code>SomeClass sc = (SomeClass)SomeObject;
SomeClass sc2 = SomeObject as SomeClass;
</code></pre>
<p>Normally, shouldn't they both be explicit casts to the specified type?</p>

[{'answer_id': 2487, 'author': 'Rytmis', 'author_id': 266, 'author_profile': 'https://Stackoverflow.com/users/266', 'pm_score': 7, 'selected': True, 'text': '<p>The former will throw an exception if the source type can\'t be cast to the target type. The latter will result in sc2 being a null reference, but no exception.</p>\n\n<p>[Edit]</p>\n\n<p>My original answer is certainly the most pronounced difference, but as Eric Lippert <a href="http://blogs.msdn.com/ericlippert/archive/2009/10/08/what-s-the-difference-between-as-and-cast-operators.aspx" rel="noreferrer">points out</a>, it\'s not the only one. Other differences include:</p>\n\n<ul>\n<li>You can\'t use the \'as\' operator to cast to a type that doesn\'t accept \'null\' as a value</li>\n<li>You can\'t use \'as\' to <em>convert</em> things, like numbers to a different representation (float to int, for example).</li>\n</ul>\n\n<p>And finally, using \'as\' vs. the cast operator, you\'re also saying "I\'m not sure if this will succeed." </p>\n'}, {'answer_id': 2489, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>The parenthetical cast throws an exception if the cast attempt fails. The "as" cast returns null if the cast attempt fails.</p>\n'}, {'answer_id': 2497, 'author': 'Steve Willard', 'author_id': 402, 'author_profile': 'https://Stackoverflow.com/users/402', 'pm_score': 1, 'selected': False, 'text': '<p>They\'ll throw different exceptions.</p>\n<p>() : NullReferenceException <br>\nas : InvalidCastException <br></p>\n<p>Which could help for debugging.</p>\n<p>The &quot;as&quot; keyword attempts to cast the object and if the cast fails, null is returned. The () cast operator will throw an exception immediately if the cast fails.</p>\n<p>Only use the C# &quot;as&quot; keyword where you are expecting the cast to fail in a non-exceptional case. If you are counting on a cast to succeed and unprepared to receive any object that would fail, you should use the () cast operator so that an appropriate and helpful exception is thrown.</p>\n<p>For code examples and a further explanation: <a href="http://blog.nerdbank.net/2008/06/when-not-to-use-c-keyword.html" rel="nofollow noreferrer">http://blog.nerdbank.net/2008/06/when-not-to-use-c-keyword.html</a></p>\n'}, {'answer_id': 2522, 'author': 'denny', 'author_id': 27, 'author_profile': 'https://Stackoverflow.com/users/27', 'pm_score': 5, 'selected': False, 'text': '<p>Also note that you can only use the as keyword with a reference type or a nullable type</p>\n\n<p>ie:</p>\n\n<pre><code>double d = 5.34;\nint i = d as int;\n</code></pre>\n\n<p>will not compile</p>\n\n<pre><code>double d = 5.34;\nint i = (int)d;\n</code></pre>\n\n<p>will compile.</p>\n'}, {'answer_id': 2614, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': 2, 'selected': False, 'text': '<p>Here is a good way to remember the process that each of them follow that I use when trying to decide which is better for my circumstance.</p>\n\n<pre><code>DateTime i = (DateTime)value;\n// is like doing\nDateTime i = value is DateTime ? value as DateTime : throw new Exception(...);\n</code></pre>\n\n<p>and the next should be easy to guess what it does</p>\n\n<pre><code>DateTime i = value as DateTime;\n</code></pre>\n\n<p>in the first case if the value cannot be cast than an exception is thrown in the second case if the value cannot be cast, i is set to null.  </p>\n\n<p>So in the first case a hard stop is made if the cast fails in the second cast a soft stop is made and you might encounter a NullReferenceException later on.</p>\n'}, {'answer_id': 5824, 'author': 'ICR', 'author_id': 214, 'author_profile': 'https://Stackoverflow.com/users/214', 'pm_score': 1, 'selected': False, 'text': "<p>It's like the difference between Parse and TryParse. You use TryParse when you expect it might fail, but when you have strong assurance it won't fail you use Parse.</p>\n"}, {'answer_id': 48238, 'author': 'Omer van Kloeten', 'author_id': 4979, 'author_profile': 'https://Stackoverflow.com/users/4979', 'pm_score': 2, 'selected': False, 'text': '<p>To expand on <a href="https://stackoverflow.com/questions/2483/casting-newtype-vs-object-as-newtype#2487">Rytmis\'s comment</a>, you can\'t use the <em>as</em> keyword for structs (Value Types), as they have no null value.</p>\n'}, {'answer_id': 75949, 'author': 'Joe', 'author_id': 13087, 'author_profile': 'https://Stackoverflow.com/users/13087', 'pm_score': 4, 'selected': False, 'text': '<p>Typecasting using "as" is of course much faster when the cast fails, as it avoids the expense of throwing an exception.</p>\n\n<p>But it is not faster when the cast succeeds.  The graph at <a href="http://www.codeproject.com/KB/cs/csharpcasts.aspx" rel="noreferrer">http://www.codeproject.com/KB/cs/csharpcasts.aspx</a> is misleading because it doesn\'t explain what it\'s measuring.</p>\n\n<p>The bottom line is:</p>\n\n<ul>\n<li><p>If you expect the cast to succeed (i.e. a failure would be exceptional), use a cast.</p></li>\n<li><p>If you don\'t know if it will succeed, use the "as" operator and test the result for null.</p></li>\n</ul>\n'}, {'answer_id': 77605, 'author': 'Adam Ruth', 'author_id': 10262, 'author_profile': 'https://Stackoverflow.com/users/10262', 'pm_score': 2, 'selected': False, 'text': '<p>For those of you with VB.NET experience, (type) is the same as DirectCast and "as type" is the same as TryCast.</p>\n'}, {'answer_id': 91669, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 2, 'selected': False, 'text': "<p>All of this applies to reference types, value types cannot use the <code>as</code> keyword as they cannot be null.</p>\n\n<pre><code>//if I know that SomeObject is an instance of SomeClass\nSomeClass sc = (SomeClass) someObject;\n\n\n//if SomeObject *might* be SomeClass\nSomeClass sc2 = someObject as SomeClass;\n</code></pre>\n\n<p>The cast syntax is quicker, but only when successful, it's much slower to fail.</p>\n\n<p>Best practice is to use <code>as</code> when you don't know the type:</p>\n\n<pre><code>//we need to know what someObject is\nSomeClass sc;\nSomeOtherClass soc;\n\n//use as to find the right type\nif( ( sc = someObject as SomeClass ) != null ) \n{\n    //do something with sc\n}\nelse if ( ( soc = someObject as SomeOtherClass ) != null ) \n{\n    //do something with soc\n}\n</code></pre>\n\n<p>However if you are absolutely sure that <code>someObject</code> is an instance of <code>SomeClass</code> then use cast.</p>\n\n<p>In .Net 2 or above generics mean that you very rarely need to have an un-typed instance of a reference class, so the latter is less often used.</p>\n"}, {'answer_id': 274026, 'author': 'Cristian Libardo', 'author_id': 16526, 'author_profile': 'https://Stackoverflow.com/users/16526', 'pm_score': 3, 'selected': False, 'text': '<p>A difference between the two approaches is that the the first ((SomeClass)obj) may cause a <a href="http://msdn.microsoft.com/en-us/library/yy580hbd(VS.80).aspx" rel="noreferrer">type converter</a> to be called.</p>\n'}, {'answer_id': 4041427, 'author': 'Andrei Rînea', 'author_id': 1796, 'author_profile': 'https://Stackoverflow.com/users/1796', 'pm_score': 3, 'selected': False, 'text': '<p>Well <strong>the \'as\' operator "helps" you bury your problem way lower</strong> because when it is provided an incompatible instance it will return null, maybe you\'ll pass that to a method which will pass it to another and so on and finally you\'ll get a NullReferenceException which will make your debugging harder.</p>\n\n<p>Don\'t abuse it. The direct cast operator is better in 99% of the cases.</p>\n'}]
['https://Stackoverflow.com/questions/2483', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/91/']