When the method is implemented in the reuse class, the method fulfills its parameters. For invalid parameters, the method should throw an exception type derived from system. argumentexception.
To build a secure and robust class library, the parameters should be copied to methods that can change the reference type.
Code
# Define Badcode
Using System;
Using System. Threading;
Class Program
{
Static Void Main ( String [] ARGs)
{
Int [] Denominators = { 1 , 2 , 3 };
Threadpool. queueuserworkitem (divide100by, denominators );
thread. sleep ( 50 );
// external variable parameters
denominators [ 2 ] = 0 ;
console. readline ();
}
Private Static Void Divide100by ( Object O)
{
# If Badcode
Int [] Denominators = ( Int []) O;
# Else
Int [] Denominatorsinput = ( Int []) O;
Int [] Denominators = New Int [Denominatorsinput. Length];
Array. Copy (denominatorsinput, denominators, denominators. Length );
# Endif
For ( Int Index = 0 ; Index < Denominators. length; index ++ )
{
If (Denominators [Index] = 0 )
{
Throw New Argumentoutofrangeexception ( " Denominators " , String . Format ( " The index {0} is 0. " , Index ));
}
}
Console. writeline ( " Verification passed " );
Thread. Sleep ( 100 );
// Here, denominators [0] is changed to 0 by external
For ( Int Index = 0 ; Index < Denominators. length; index ++ )
{
Console. writeline ( " 100/{0 }={ 1} " , Denominators [Index], 100 / Denominators [Index]);
}
}
}
The solution is already on the top