Skip to Content
C# Cookbook, 2nd Edition
book

C# Cookbook, 2nd Edition

by Jay Hilyard, Stephen Teilhet
January 2006
Intermediate to advanced
1184 pages
43h 23m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook, 2nd Edition
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Safely Performing a Narrowing Numeric Cast
|
15
Solution
The simplest way to do this check is to use the checked keyword. The following
method accepts two
long data types and attempts to add them together. The result is
stuffed into an
int data type. If an overflow condition exists, the OverflowException
is thrown:
using System;
public void UseChecked(long lhs, long rhs)
{
int result = 0;
try
{
result = checked((int)(lhs + rhs));
}
catch (OverflowException e)
{
// Handle overflow exception here.
}
}
This is the simplest method. However, if you do not want the overhead of throwing an
exception and having to wrap a lot of code in
try/catch blocks to handle the overflow
condition, you can use the
MaxValue and MinValue fields of each type. A check using
these fields can be done prior to the conversion to insure that no loss of information
occurs. If this does occur, the code can inform the application that this cast will cause a
loss of information. You can use the following conditional statement to determine
whether
sourceValue can be cast to a short without losing any information:
// Our two variables are declared and initialized.
int sourceValue = 34000;
short destinationValue = 0;
// Determine if sourceValue will lose information in a cast to a short.
if (sourceValue <= short.MaxValue && sourceValue ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C# Cookbook

C# Cookbook

Joe Mayo
C# Cookbook

C# Cookbook

Stephen Teilhet, Jay Hilyard
C# 3.0 Cookbook, 3rd Edition

C# 3.0 Cookbook, 3rd Edition

Jay Hilyard, Stephen Teilhet
C# 6.0 Cookbook, 4th Edition

C# 6.0 Cookbook, 4th Edition

Stephen Teilhet, Jay Hilyard

Publisher Resources

ISBN: 0596100639Catalog PageErrata