Click here to Skip to main content
15,891,951 members
Home / Discussions / C#
   

C#

 
AnswerRe: Foreach Item in... Pin
Pedram Behroozi23-Nov-08 21:34
Pedram Behroozi23-Nov-08 21:34 
GeneralRe: Foreach Item in... Pin
jas0n2323-Nov-08 21:51
jas0n2323-Nov-08 21:51 
GeneralWhat's the difference using a cast or C#'s 'as' [modified] Pin
Martin Hart Turner23-Nov-08 21:17
Martin Hart Turner23-Nov-08 21:17 
GeneralRe: What's the difference using a cast or C#'s 'as' Pin
Baconbutty23-Nov-08 21:36
Baconbutty23-Nov-08 21:36 
GeneralRe: What's the difference using a cast or C#'s 'as' Pin
leppie23-Nov-08 22:22
leppie23-Nov-08 22:22 
GeneralRe: What's the difference using a cast or C#'s 'as' Pin
Simon P Stevens23-Nov-08 22:16
Simon P Stevens23-Nov-08 22:16 
GeneralRe: What's the difference using a cast or C#'s 'as' Pin
Martin Hart Turner23-Nov-08 22:38
Martin Hart Turner23-Nov-08 22:38 
GeneralRe: What's the difference using a cast or C#'s 'as' Pin
James Simpson23-Nov-08 22:48
James Simpson23-Nov-08 22:48 
'as' is used when the instance you are checking may not of the expected type. E.g Your expecting an 'object' but want it to be a specific type (or maybe one of many types) for a function parameter but the caller may pass in a type you dont want or expect, so you can treat is as null by casting it with the as keyword.

An example of this may be when overriding the Equals method on System.Object

The () cast method is more effecient becuase as compiles down to a set of IL instructions that include checking the type before attempting the cast, or returning null, eg:

Customer customer = null;
if(e.SomeObject is Customer)
    customer = (Customer)e.SomeObject;
else
    customer = null;


So, I would say 'as' has its place, but its not a replacement for a () cast. Its probably used by certain people becuase they consider it easier to read, rather than actually need to use it.

James

James Simpson
Web Developer
imebgo@hotmail.com

P S - This is what part of the alphabet would look like if Q and R were eliminated
Mitch Hedberg

GeneralRe: What's the difference using a cast or C#'s 'as' Pin
Simon P Stevens23-Nov-08 22:59
Simon P Stevens23-Nov-08 22:59 
GeneralRe: What's the difference using a cast or C#'s 'as' Pin
James Simpson23-Nov-08 23:07
James Simpson23-Nov-08 23:07 
GeneralRe: What's the difference using a cast or C#'s 'as' Pin
Simon P Stevens23-Nov-08 23:12
Simon P Stevens23-Nov-08 23:12 
GeneralRe: What's the difference using a cast or C#'s 'as' Pin
Simon P Stevens23-Nov-08 22:57
Simon P Stevens23-Nov-08 22:57 
GeneralRe: What's the difference using a cast or C#'s 'as' Pin
Martin Hart Turner23-Nov-08 23:37
Martin Hart Turner23-Nov-08 23:37 
GeneralRe: What's the difference using a cast or C#'s 'as' Pin
Chris Maunder23-Nov-08 22:53
cofounderChris Maunder23-Nov-08 22:53 
QuestionReplaces "[fieldname]" in the HTML template document with data from the corresponding column of the SQLite table. Pin
Tsodgorhuu23-Nov-08 21:00
Tsodgorhuu23-Nov-08 21:00 
AnswerRe: Replaces "[fieldname]" in the HTML template document with data from the corresponding column of the SQLite table. Pin
Thomas Weller23-Nov-08 21:30
Thomas Weller23-Nov-08 21:30 
AnswerRe: Replaces "[fieldname]" in the HTML template document with data from the corresponding column of the SQLite table. Pin
Ashfield23-Nov-08 21:50
Ashfield23-Nov-08 21:50 
GeneralRe: Replaces "[fieldname]" in the HTML template document with data from the corresponding column of the SQLite table. Pin
Guffa23-Nov-08 23:09
Guffa23-Nov-08 23:09 
QuestionA program to delete first n bytes in a file Pin
Matjaz-xyz23-Nov-08 20:47
Matjaz-xyz23-Nov-08 20:47 
AnswerRe: A program to delete first n bytes in a file Pin
Christian Graus23-Nov-08 20:50
protectorChristian Graus23-Nov-08 20:50 
GeneralRe: A program to delete first n bytes in a file Pin
Matjaz-xyz23-Nov-08 21:00
Matjaz-xyz23-Nov-08 21:00 
GeneralRe: A program to delete first n bytes in a file Pin
Guffa23-Nov-08 21:38
Guffa23-Nov-08 21:38 
GeneralRe: A program to delete first n bytes in a file Pin
Matjaz-xyz23-Nov-08 21:53
Matjaz-xyz23-Nov-08 21:53 
GeneralRe: A program to delete first n bytes in a file Pin
Giorgi Dalakishvili23-Nov-08 22:00
mentorGiorgi Dalakishvili23-Nov-08 22:00 
GeneralRe: A program to delete first n bytes in a file Pin
Matjaz-xyz23-Nov-08 22:23
Matjaz-xyz23-Nov-08 22:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.