diff --git a/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/IndexOutOfRangeExceptionProject.csproj b/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/IndexOutOfRangeExceptionProject.csproj index bb960d5e67..38042621af 100644 --- a/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/IndexOutOfRangeExceptionProject.csproj +++ b/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/IndexOutOfRangeExceptionProject.csproj @@ -2,12 +2,8 @@ Exe - net6.0 + net10.0 enable - - - - diff --git a/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/Program.cs b/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/Program.cs index aae2d09df7..333c86ce4d 100644 --- a/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/Program.cs +++ b/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/Program.cs @@ -1,30 +1,27 @@ -namespace IndexOutOfRangeExceptionProject -{ - internal class Program - { - private static void Main(string[] args) - { - UtilityHelper utilityHelper = new(); - - //1) Array - Upper Bound - should be less that the array length - utilityHelper.UpperBoundMethod(false); - - //2) Array with custom lower bound - utilityHelper.CustomLowerBoundMethod(2, false); - - //3) Incorrect Arguments - Console.WriteLine("Please enter a search argument!!!"); - var searchArg = Convert.ToInt32(Console.ReadLine()); - utilityHelper.ListIndexOfMethod(searchArg, false); - - //4) 2 arrays with different dimensions - utilityHelper.AssignFirstArrayElementToSecondArrayMethod(false); - - //5) Getting confused between the index and value at that index - utilityHelper.IndexAndValueMethod(false); - - //6 -> DataTable example - utilityHelper.DataTableMethod(false); - } - } -} \ No newline at end of file +using IndexOutOfRangeExceptionProject; + +UtilityHelper utilityHelper = new(); + +//1) Array - Upper Bound - should be less than the array length +utilityHelper.UpperBoundMethod(false); + +//2) Array with custom lower bound +utilityHelper.CustomLowerBoundMethod(2, false); + +//3) Incorrect Arguments +Console.WriteLine("Please enter a search argument: "); +var searchArg = Convert.ToInt32(Console.ReadLine()); +utilityHelper.ListIndexOfMethod(searchArg, false); + +//4) 2 arrays with different dimensions +utilityHelper.AssignFirstArrayElementToSecondArrayMethod(false); + +//5) Getting confused between the index and value at that index +utilityHelper.IndexAndValueMethod(false); + +//6) DataTable example +utilityHelper.DataTableMethod(false); + +//7) Index from end, ranges and ElementAtOrDefault +utilityHelper.IndexFromEndMethod(false); +utilityHelper.RangeMethod(false); diff --git a/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/Repository/StaticDataRepository.cs b/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/Repository/StaticDataRepository.cs index 7e406035f6..ce5607b7bf 100644 --- a/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/Repository/StaticDataRepository.cs +++ b/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/Repository/StaticDataRepository.cs @@ -1,25 +1,24 @@ using System.Data; -namespace IndexOutOfRangeExceptionProject.Repository +namespace IndexOutOfRangeExceptionProject.Repository; + +public class StaticDataRepository { - public class StaticDataRepository + public static DataTable GetGroceries() { - public static DataTable GetGroceries() + var dataTable = new DataTable { - var dataTable = new DataTable - { - TableName = "Groceries" - }; - dataTable.Columns.Add("Id", typeof(int)); - dataTable.Columns.Add("Name", typeof(string)); - dataTable.Columns.Add("Description", typeof(string)); + TableName = "Groceries" + }; + dataTable.Columns.Add("Id", typeof(int)); + dataTable.Columns.Add("Name", typeof(string)); + dataTable.Columns.Add("Description", typeof(string)); - dataTable.Rows.Add(1, "Macaroni", "Tasty Pasta"); - dataTable.Rows.Add(2, "Ramen", "Tasty Noodles"); - dataTable.Rows.Add(3, "Figaro Oil", "Olive Oil"); - dataTable.Rows.Add(4, "XYZ Lentils", "Nutritious Pulses"); + dataTable.Rows.Add(1, "Macaroni", "Tasty Pasta"); + dataTable.Rows.Add(2, "Ramen", "Tasty Noodles"); + dataTable.Rows.Add(3, "Figaro Oil", "Olive Oil"); + dataTable.Rows.Add(4, "XYZ Lentils", "Nutritious Pulses"); - return dataTable; - } + return dataTable; } -} \ No newline at end of file +} diff --git a/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/UtilityHelper.cs b/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/UtilityHelper.cs index 40a2d131ee..a3d8eb7282 100644 --- a/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/UtilityHelper.cs +++ b/exception-handling/IndexOutOfRangeExceptionSolution/IndexOutOfRangeExceptionProject/UtilityHelper.cs @@ -1,154 +1,173 @@ using IndexOutOfRangeExceptionProject.Repository; using System.Data; -namespace IndexOutOfRangeExceptionProject +namespace IndexOutOfRangeExceptionProject; + +public class UtilityHelper { - public class UtilityHelper + public int[] NumbersArray { get; set; } + public Array CustomLowerBoundArray { get; set; } + public List MultiplesOfFive { get; set; } + public int[] Array1 { get; set; } + public int[] Array2 { get; set; } + public int[] OddNumbersArray { get; set; } + public DataTable Groceries { get; set; } + + public UtilityHelper() { - public int[] NumbersArray { get; set; } - public Array CustomLowerBoundArray { get; set; } - public List MultiplesOfFive { get; set; } - public int[] Array1 { get; set; } - public int[] Array2 { get; set; } - public int[] OddNumbersArray { get; set; } - public DataTable Groceries { get; set; } + NumbersArray = new int[] { 1, 2, 3, 4, 5 }; + CustomLowerBoundArray = Array.CreateInstance(typeof(int), new int[] { 5 }, new int[] { 1 }); + MultiplesOfFive = new List { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 }; + Array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + OddNumbersArray = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }; + Groceries = StaticDataRepository.GetGroceries(); + } - public UtilityHelper() + public void UpperBoundMethod(bool forcefullyRaiseException) + { + if (forcefullyRaiseException) { - NumbersArray = new int[] { 1, 2, 3, 4, 5 }; - CustomLowerBoundArray = Array.CreateInstance(typeof(int), new int[] { 5 }, new int[] { 1 }); - MultiplesOfFive = new List { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 }; - Array1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; - OddNumbersArray = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }; - Groceries = StaticDataRepository.GetGroceries(); + for (var i = 0; i <= NumbersArray.Length; i++) + { + Console.WriteLine(NumbersArray[i]); + } } - - public void UpperBoundMethod(bool forcefullyRaiseException) + else { - if (forcefullyRaiseException) + for (var i = 0; i < NumbersArray.Length; i++) { - for (var i = 0; i <= NumbersArray.Length; i++) - { - Console.WriteLine(NumbersArray[i]); - } + Console.WriteLine(NumbersArray[i]); } - else + } + } + + public void CustomLowerBoundMethod(int input, bool forcefullyRaiseException) + { + if (forcefullyRaiseException) + { + for (var i = 0; i < CustomLowerBoundArray.Length; i++) { - for (var i = 0; i < NumbersArray.Length; i++) - { - Console.WriteLine(NumbersArray[i]); - } + CustomLowerBoundArray.SetValue(input, i); + input *= 5; } } - - public void CustomLowerBoundMethod(int input, bool forcefullyRaiseException) + else { - if (forcefullyRaiseException) + for (var i = CustomLowerBoundArray.GetLowerBound(0); i <= CustomLowerBoundArray.GetUpperBound(0); i++) { - for (var i = 0; i < CustomLowerBoundArray.Length; i++) - { - CustomLowerBoundArray.SetValue(input, i); - input *= 5; - } + CustomLowerBoundArray.SetValue(input, i); + input *= 5; } - else + for (var i = CustomLowerBoundArray.GetLowerBound(0); i <= CustomLowerBoundArray.GetUpperBound(0); i++) { - for (var i = CustomLowerBoundArray.GetLowerBound(0); i <= CustomLowerBoundArray.GetUpperBound(0); i++) - { - CustomLowerBoundArray.SetValue(input, i); - input *= 5; - } - for (var i = CustomLowerBoundArray.GetLowerBound(0); i <= CustomLowerBoundArray.GetUpperBound(0); i++) - { - Console.WriteLine(CustomLowerBoundArray.GetValue(i)); - } + Console.WriteLine(CustomLowerBoundArray.GetValue(i)); } } + } - public void ListIndexOfMethod(int searchArgument, bool forcefullyRaiseException) - { - var startIndex = MultiplesOfFive.IndexOf(searchArgument); + public void ListIndexOfMethod(int searchArgument, bool forcefullyRaiseException) + { + var startIndex = MultiplesOfFive.IndexOf(searchArgument); - if (forcefullyRaiseException) + if (forcefullyRaiseException) + { + Console.WriteLine($"Let's display the multiples of 5 starting at {searchArgument}"); + for (var i = startIndex; i < MultiplesOfFive.Count; i++) { - Console.WriteLine($"Let's display the even numbers greater than {searchArgument}"); - for (var i = startIndex; i < MultiplesOfFive.Count; i++) - { - Console.WriteLine(MultiplesOfFive[i]); - } + Console.WriteLine(MultiplesOfFive[i]); + } + } + else + { + if (startIndex < 0) + { + Console.WriteLine($"The number {searchArgument} does not exist in the list."); } else { - if (startIndex < 0) - { - Console.WriteLine($"The number {searchArgument} does not exist in the list."); - } - else + Console.WriteLine($"Let's display the multiples of 5 starting at {searchArgument}"); + for (var i = startIndex; i < MultiplesOfFive.Count; i++) { - Console.WriteLine($"Let's display the even numbers greater than {searchArgument}"); - for (var i = startIndex; i < MultiplesOfFive.Count; i++) - { - Console.WriteLine(MultiplesOfFive[i]); - } + Console.WriteLine(MultiplesOfFive[i]); } } } + } - public void AssignFirstArrayElementToSecondArrayMethod(bool forcefullyRaiseException) + public void AssignFirstArrayElementToSecondArrayMethod(bool forcefullyRaiseException) + { + if (forcefullyRaiseException) { - if (forcefullyRaiseException) - { - Array2 = new int[7]; - Array2[Array1.Length - 1] = Array1[Array1.Length - 1]; - Console.WriteLine($"The last element of array2 is: {Array2[Array2.Length - 1]}"); - } - else - { - Array2 = new int[Array1.Length]; - Array2[Array1.Length - 1] = Array1[Array1.Length - 1]; - Console.WriteLine($"The last element of array2 is: {Array2[Array2.Length - 1]}"); - } + Array2 = new int[7]; + Array2[Array1.Length - 1] = Array1[Array1.Length - 1]; + Console.WriteLine($"The last element of array2 is: {Array2[Array2.Length - 1]}"); } + else + { + Array2 = new int[Array1.Length]; + Array2[Array1.Length - 1] = Array1[Array1.Length - 1]; + Console.WriteLine($"The last element of array2 is: {Array2[Array2.Length - 1]}"); + } + } - public void IndexAndValueMethod(bool forcefullyRaiseException) + public void IndexAndValueMethod(bool forcefullyRaiseException) + { + if (forcefullyRaiseException) { - if (forcefullyRaiseException) + foreach (var number in OddNumbersArray) { - foreach (var number in OddNumbersArray) - { - Console.WriteLine($"The odd number is: {OddNumbersArray[number]}"); - } + Console.WriteLine($"The odd number is: {OddNumbersArray[number]}"); } - else + } + else + { + foreach (var number in OddNumbersArray) { - foreach (var number in OddNumbersArray) - { - Console.WriteLine($"The odd number is: {number}"); - } + Console.WriteLine($"The odd number is: {number}"); } } + } - public void DataTableMethod(bool forcefullyRaiseException) + public void DataTableMethod(bool forcefullyRaiseException) + { + if (forcefullyRaiseException) { - if (forcefullyRaiseException) + for (var i = 0; i < Groceries.Rows.Count; i++) { - for (var i = 0; i < Groceries.Rows.Count; i++) - { - var row = Groceries.Rows[i]; - Console.WriteLine($"{row[0]}\t {row[1]}\t {row[2]}\t {row[3]}"); - } + var row = Groceries.Rows[i]; + Console.WriteLine($"{row[0]}\t {row[1]}\t {row[2]}\t {row[3]}"); } - else + } + else + { + foreach (DataRow grocery in Groceries.Rows) { - foreach (DataRow grocery in Groceries.Rows) + foreach (var item in grocery.ItemArray) { - foreach (var item in grocery.ItemArray) - { - Console.WriteLine($"{item}"); - } - Console.WriteLine("********************"); + Console.WriteLine($"{item}"); } + Console.WriteLine("********************"); } } } -} \ No newline at end of file + + public void IndexFromEndMethod(bool forcefullyRaiseException) + { + if (forcefullyRaiseException) + { + Console.WriteLine(NumbersArray[^0]); + } + else + { + Console.WriteLine($"The last element is: {NumbersArray[^1]}"); + Console.WriteLine($"The last three elements are: {string.Join(", ", NumbersArray[^3..])}"); + Console.WriteLine($"The tenth element or the default: {NumbersArray.ElementAtOrDefault(9)}"); + } + } + + public void RangeMethod(bool forcefullyRaiseException) + { + var range = forcefullyRaiseException ? 3..7 : 3..5; + Console.WriteLine(string.Join(", ", NumbersArray[range])); + } +} diff --git a/exception-handling/IndexOutOfRangeExceptionSolution/Tests/IndexOutOfRangeExceptionUnitTest.cs b/exception-handling/IndexOutOfRangeExceptionSolution/Tests/IndexOutOfRangeExceptionUnitTest.cs index 700f1a4812..65dd1497f8 100644 --- a/exception-handling/IndexOutOfRangeExceptionSolution/Tests/IndexOutOfRangeExceptionUnitTest.cs +++ b/exception-handling/IndexOutOfRangeExceptionSolution/Tests/IndexOutOfRangeExceptionUnitTest.cs @@ -1,77 +1,98 @@ using IndexOutOfRangeExceptionProject; using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace Tests +namespace Tests; + +[TestClass] +public class IndexOutOfRangeExceptionUnitTest { - [TestClass] - public class IndexOutOfRangeExceptionUnitTest + private readonly UtilityHelper utilityHelper = new(); + + [TestMethod] + public void WhenExceptionFlagIsTrueForUpperBoundMethod_ThenRaiseException() { - private readonly UtilityHelper utilityHelper = new(); + var testArray = () => + { + utilityHelper.UpperBoundMethod(true); + }; - [TestMethod] - public void WhenExceptionFlagIsTrueForUpperBoundMethod_ThenRaiseException() + Assert.ThrowsExactly(testArray); + } + + [TestMethod] + public void WhenExceptionFlagIsTrueForCustomLowerBoundMethod_ThenRaiseException() + { + var testArray = () => { - var testArray = () => - { - utilityHelper.UpperBoundMethod(true); - }; + utilityHelper.CustomLowerBoundMethod(2, true); + }; - Assert.ThrowsException(testArray); - } + Assert.ThrowsExactly(testArray); + } - [TestMethod] - public void WhenExceptionFlagIsTrueForCustomLowerBoundMethod_ThenRaiseException() + [TestMethod] + public void WhenExceptionFlagIsTrueAndInvalidSearchArgumentForListIndexOfMethod_ThenRaiseException() + { + var testArray = () => { - var testArray = () => - { - utilityHelper.CustomLowerBoundMethod(2, true); - }; + utilityHelper.ListIndexOfMethod(11, true); + }; - Assert.ThrowsException(testArray); - } + Assert.ThrowsExactly(testArray); + } - [TestMethod] - public void WhenExceptionFlagIsTrueAndInvalidSearchArgumentForListIndexOfMethod_ThenRaiseException() + [TestMethod] + public void WhenExceptionFlagIsTrueForAssignFirstArrayElementToSecondArrayMethod_ThenRaiseException() + { + var testArray = () => { - var testArray = () => - { - utilityHelper.ListIndexOfMethod(11, true); - }; + utilityHelper.AssignFirstArrayElementToSecondArrayMethod(true); + }; - Assert.ThrowsException(testArray); - } + Assert.ThrowsExactly(testArray); + } - [TestMethod] - public void WhenExceptionFlagIsTrueForAssignFirstArrayElementToSecondArrayMethod_ThenRaiseException() + [TestMethod] + public void WhenExceptionFlagIsTrueForIndexAndValueMethod_ThenRaiseException() + { + var testArray = () => + { + utilityHelper.IndexAndValueMethod(true); + }; + + Assert.ThrowsExactly(testArray); + } + + [TestMethod] + public void WhenExceptionFlagIsTrueForDataTableMethod_ThenRaiseException() + { + var testArray = () => { - var testArray = () => - { - utilityHelper.AssignFirstArrayElementToSecondArrayMethod(true); - }; + utilityHelper.DataTableMethod(true); + }; - Assert.ThrowsException(testArray); - } + Assert.ThrowsExactly(testArray); + } - [TestMethod] - public void WhenExceptionFlagIsTrueForIndexAndValueMethod_ThenRaiseException() + [TestMethod] + public void WhenExceptionFlagIsTrueForIndexFromEndMethod_ThenRaiseException() + { + var testArray = () => { - var testArray = () => - { - utilityHelper.IndexAndValueMethod(true); - }; + utilityHelper.IndexFromEndMethod(true); + }; - Assert.ThrowsException(testArray); - } + Assert.ThrowsExactly(testArray); + } - [TestMethod] - public void WhenExceptionFlagIsTrueForDataTableMethod_ThenRaiseException() + [TestMethod] + public void WhenExceptionFlagIsTrueForRangeMethod_ThenRaiseArgumentOutOfRangeException() + { + var testArray = () => { - var testArray = () => - { - utilityHelper.DataTableMethod(true); - }; + utilityHelper.RangeMethod(true); + }; - Assert.ThrowsException(testArray); - } + Assert.ThrowsExactly(testArray); } -} \ No newline at end of file +} diff --git a/exception-handling/IndexOutOfRangeExceptionSolution/Tests/Tests.csproj b/exception-handling/IndexOutOfRangeExceptionSolution/Tests/Tests.csproj index 611c81d5c3..a8cc6bd0f3 100644 --- a/exception-handling/IndexOutOfRangeExceptionSolution/Tests/Tests.csproj +++ b/exception-handling/IndexOutOfRangeExceptionSolution/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net10.0 enable enable @@ -9,10 +9,10 @@ - - - - + + + +