#1,128 – Disable a Compile-Time Warning for Entire Project

You can use #pragma warning to selectively disable certain warnings that occur when compiling your code.  When #pragma warning disable is encountered, the specified warning will be disabled for the current file, from the point where the pragma appears until the end of the file, or until a #pragma warning restore is encountered for the same warning.

If you want to disable a particular warning for all files compiled in a project, rather than just within a single file, you can add the warning number to the Suppress warnings field on the Build tab in the project properties window.  This will suppress the specified warning for the entire project.

1128-001

1128-002

Advertisements

#1,127 – Where to Find Compiler Warning Numbers

You can use #pragma warning to selectively disable certain warnings that occur when compiling your code.

The #pragma warning line requires knowing the specific warning number, which is not displayed in the list of warnings on the Error List tab.

1127-001

To find the warning number, you need to switch to the Output tab and look at the full text of the warning.  In the example below, we see that the warning about an event that is never used is warning #67.

1127-002

Once you know the warning number, you can use it with a #pragma warning.

#pragma warning disable 67
        public event EventHandler CanBarkChanged;
#pragma warning restore 67

#1,126 – Rewriting a Loop to Avoid continue Statement

The continue statement allows jumping to end of the body of a loop, skipping the rest of the code within the loop.  Either the next iteration of the loop executes, or the code following the loop statement executes.

            List<Dog> myDogs = new List<Dog>
            {
                new Dog {Name = "Kirby", Age = 5},
                new Dog {Name = "Jack", Age = 17},
                new Dog {Name = "Ruby", Age = 2}
            };

            foreach (Dog d in myDogs)
            {
                if (d.Age > 15)
                    continue;

                Console.WriteLine("{0} doing a trick", d.Name);
                d.DoATrick();
            }

1126-001
In most cases, you can rewrite the body of a loop and avoid using a continue statement by using an if statement.  The end result is generally more readable.

            foreach (Dog d in myDogs)
            {
                if (d.Age <= 15)
                {
                    Console.WriteLine("{0} doing a trick", d.Name);
                    d.DoATrick();
                }
            }

#1,125 – Use of break and continue Statements

You can use the break statement to break out of a loop or switch statement, resuming execution at the endpoint of the enclosing statement.

You can use break within a switch statement or within while, do, for or foreach loops.

You can use the continue statement to jump unconditionally to the endpoint of the body of statements enclosed within a loop.  This will either cause execution to continue with the next iteration of the loop or will continue execution after the loop if the final iteration was being executed.

You can use continue within a while, do, for or foreach loop.

#1,124 – Iterate through Jagged Array with Nested foreach

You can’t use the foreach statement to directly iterate over all elements in a jagged array.  You can, however, iterate through the jagged array using nested foreach statements.  Each foreach statement independently iterates over one dimension of the jagged array.

            // Jagged array - 3 elements, each of which is array of int
            int[][] nums = new int[3][];

            nums[0] = new int[4];
            nums[2] = new int[3];

            for (int i = 0; i < 4; i++)
                nums[0][i] = i + 1;

            for (int i = 0; i < 3; i++)
                nums[2][i] = i + 101;

            // Iterate using foreach
            foreach (int[] intArray in nums)
            {
                if (intArray != null)
                {
                    Console.WriteLine("Array with {0} elements", intArray.Length);
                    foreach (int n in intArray)
                        Console.WriteLine("  {0}", n);
                }
                else
                    Console.WriteLine("Found null array");
            }

1124-001

#1,123 – Using foreach to Iterate on a Multidimensional Array

You can use the foreach statement to iterate through all elements in a multidimensional array.  The elements are iterated in a row-major order.  That is, all elements in the first row are enumerated, followed by the second row, etc.

            // 2-dimensional array
            string[,] twoByThree = new string[2, 3];

            // Populate with standard for loop
            for (int row = 0; row < 2; row++)
                for (int col = 0; col < 3; col++)
                    twoByThree[row, col] = string.Format("{0}/{1}", row + 1, col + 1);

            // Iterate using foreach
            foreach (string s in twoByThree)
                Console.WriteLine(s);

1123-001

#1,122 – It’s Okay to Capture Variables within a foreach Loop

Within a foreach loop, you capture an iteration variable when you save it for later use.  In versions of C# earlier than 5.0, this led to problems and unexpected behavior.  What ended up being captured was multiple copies of the last value of the iteration variable.

This has been changed in C# 5.0 so that you can capture an iteration variable within a foreach loop and get the expected behavior.

            string[] beatles = {"George", "John", "Paul", "Ringo"};

            List<Action> actionList = new List<Action>();
            List<string> guyList = new List<string>();

            foreach (string guy in beatles)
            {
                guyList.Add(guy);
                actionList.Add(() => Console.WriteLine(guy));
            }

            foreach (string guyInList in guyList)
                Console.WriteLine(guyInList);

            Console.WriteLine("===");

            foreach (Action a in actionList)
                a();

1122-001