Skip to main content

Posts

Check Statements in .NET

Anytime a C# expression (I presume the same goes for VB) contains a non-constant value, the compiler will not check for out-of-bounds values. For example, if you initialize an integer as the sum of two values, one of which is outside the bounds for integers (-2,147,483,648 to 2,147,483,647), a runtime overflow exception will be generated, but not a compile error. Apparently checking for out-of-bounds values is computationally intensive and most of the time simply isn't worth the time/energy. However, if it is critical that your application generate a compile error for out-of-bounds values, you can use the checked keyword, which is actually classified as a primary operator in C# by Microsoft. You can put checked() around a statement you know might cause an overflow exception or checked{} around a block of code. You can also create a CheckedMethod() or if you want to explicitly avoid overflow checking, use the unchecked keyword or use UncheckedMethod() . Check (pun intended...
Recent posts

Merging Files into a Single .NET Assembly

.NET assemblies can be single-file or multifile.   As I've been preparing to take Microsoft Exam 70-483, I came across the Al.exe command, a tool which is installed along with Visual Studio. Al stands for "Assembly Linker" and when you run it, it "links" different manifest or resource files together into a single assembly. If you work with .NET at all, you probably have a pretty good idea of what an assembly is, but here's a simple definition from Stack Overflow : "A chunk of (precompiled) code that can be executed by the .NET runtime environment. A .NET program consists of one or more assemblies." -Adrian Grigore Here's a more technical definition from Wikipedia :  "A compiled code library used for deployment, versioning, and security."  Normally when you create a new project in Visual Studio, VS pretty much creates the assembly for you. You don't have to worry about merging different files together using the c...

Programming in R

Image saved from my new ShinyApp Over the past few days I've been playing around with R, a statistical language which is essentially an offshoot of S, which was created in the 1980's. R has some powerful features which enable to it rapidly create meaningful graphical illustrations. I've really enjoyed manipulating some of the datasets on Kaggle with it. Here's a ShinyApp I published to demonstrate a simple R program to track where different plant and animal species live within the US national parks.

How NOT To Write Code / How To Decipher Bad Code

Confusing code is bad code. In this post I'll be talking about one of the worst coding practices: using nondescript variables. As an example, we'll be looking at a simple problem from Project Euler (#28). I'll give you an example using really bad code and then we'll look at how we can decipher it and make it more clear and understandable. First, here's the problem: Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows: 21  22 23 24  25 20   7   8   9  10 19  6   1   2 11 18   5   4   3  12 17  16 15 14  13 It can be verified that the sum of the numbers on the diagonals is 101. What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way? Ok, so now check out this solution:             int i = 1; int j = 1; int s = 0; ...

Project Euler Problem # 25 1000-digit Fibonacci number

Spoiler Alert: As the About page of Project Euler says, "Real learning is an active process and seeing how it is done is a long way from experiencing that epiphany of discovery." If you are currently attempting to solve this problem in the archives, please do not continue to read this post. I personally worked through all the problems I post on this blog and it has brought me immense satisfaction. Problem: The Fibonacci sequence is defined by the recurrence relation: Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be: F1 = 1 F2 = 1 F3 = 2 F4 = 3 F5 = 5 F6 = 8 F7 = 13 F8 = 21 F9 = 34 F10 = 55 F11 = 89 F12 = 144 The 12th term, F12, is the first term to contain three digits. What is the index of the first term in the Fibonacci sequence to contain 1000 digits? Solution: Using the BigInteger class, this problem can be solved very simply using C#:             //Time the process         ...

Project Euler Problem #19 Counting Sundays

Spoiler Alert: As the About page of Project Euler says, "Real learning is an active process and seeing how it is done is a long way from experiencing that epiphany of discovery." If you are currently attempting to solve this problem in the archives, please do not continue to read this post. I personally worked through all the problems I post on this blog and it has brought me immense satisfaction.  Problem: You are given the following information, but you may prefer to do some research for yourself. 1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? Solution:  Although this problem sounds a little ...

Project Euler Problem #1 Multiples of 3 and 5

Spoiler Alert: As the About page of Project Euler says, "Real learning is an active process and seeing how it is done is a long way from experiencing that epiphany of discovery." If you are currently attempting to solve this problem in the archives, please do not continue to read this post. I personally worked through all the problems I post on this blog and it has brought me immense satisfaction.  Problem:  If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. Solution:  This problem can be solved brute-force without too much difficulty by simply doing a for loop from 1 to 999, incrementing by one each time, checking each number to see if it is divisible by either 3 or 5, and if so, adding it to a list. Once the for loop terminates, we can sum all the numbers in the list.  In C#, you do something like this:  ...