-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMidterm Proj
More file actions
125 lines (108 loc) · 6.31 KB
/
Midterm Proj
File metadata and controls
125 lines (108 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29709.97
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSC205 Midterm", "CSC205 Midterm.csproj", "{28935FCA-26BB-4455-A3F2-7F6DC99535D9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{28935FCA-26BB-4455-A3F2-7F6DC99535D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28935FCA-26BB-4455-A3F2-7F6DC99535D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28935FCA-26BB-4455-A3F2-7F6DC99535D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28935FCA-26BB-4455-A3F2-7F6DC99535D9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {911FB47A-8E1B-4C2A-83BE-DAFE01EA254F}
EndGlobalSection
EndGlobal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSC205_Midterm
{
class Program
{
//Namespace #1.0: System.Collections.Generic
/*Description #1.0: This Namespace contains interfaces and classes that define generic collections.
We can create strongly typed collections with better type safety and performance than non-generic strongly typed connections.*/
//Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic?view=netframework-4.8
#region "System.Collections.Generic"
public static void Main(string[] args)
{
List<string> catFact = new List<string>();
{
//Function #1.1: List<T>.ForEach(Action<T>) written as catFact.ForEach(Print)
/*Description #1.1: The ForEach portion completes the specified action for each element in the list.
Description #1.1: I designated the action to be performed by assigning it the "Print" name (doesnt matter what name you put
Its purely a variable to be called) and telling it to Console.WriteLine each element in the list. has to be used AFTER the list is declared. */
//Reference 1.1: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.foreach?view=netframework-4.8
void Print(string s)
{
Console.WriteLine(s);
}
catFact.ForEach(Print);
//Property info: int List<string>.Capacity {get; set;}
/*Extra Stuff: catFact.Capacity Gets or sets the total number of elements the data structure can hold without resizing.
* In this case I'm just getting the total amount of elements that can be held, not how many are present. */
Console.WriteLine("Capacity of this list is currently: {0}", catFact.Capacity);
//Function #1.2: List<string>.Add
/* Description #1.2: is for me to add elements to the new list that I've designated.
Each addition is to the last position within the list and memory is automatically allocated.*/
Console.WriteLine("Now inserting values into the List...");
catFact.Add("tacocat");
catFact.Add("spelled");
catFact.Add("backwards");
catFact.Add("is");
catFact.Add("tacocat");
//Extra stuff
Console.WriteLine("Capacity of this list is currently: {0}", catFact.Capacity);
}
//Reference #1.2: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.add?view=netframework-4.8
//Function #1.3: List<T>.Insert(Int32, T)
//Description #1.3: catFact.Insert(index, Thing to insert) allows me to insert an element into the list at a point I designate starting from a 0 based index.
catFact.Insert(4, "still");
//Asking for user defined value for my third function.
Console.Write("Enter a word you want to check for: ");
//Assigning the thing to be checked as a variable to be used in the 1.4 function.
string checker = Console.ReadLine();
//Function #1.4: bool list<string>.Contains
/*Description #1.4: Purpose is to query a list to check if a particular element is present; Returns a true or false value.
I've chosen to have the user define the element they want to check for.*/
// Reference #1.4: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.contains?view=netframework-4.8
bool rtn= catFact.Contains(checker);
Console.WriteLine($"True equals present, False equals not present: {rtn}");
Console.WriteLine("Removing space not filled by an element in the array...");
//Function #1.5: List<T>.TrimExcess
//Description #1.5: The catFact.TrimExcess function removes extra capacity allocated from the list that isn't populated with an element.
//Trimming and then displaying the current Capacity is one way of checking for total elements in the list without using the List<T>.Count Property
//Reference #1.5: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.trimexcess?view=netframework-4.8
catFact.TrimExcess();
Console.WriteLine("There are currently {0} elements in the list, and the capacity is {0}.", catFact.Capacity);
//Extra stuff: comparing values to the Count property.
int cnt = catFact.Count();
Console.WriteLine($"The Count property indicates there are {cnt} elements in the list");
//Console.WriteLine("This is the count property, and there are {} elements in the list.",cnt);
Console.Read();
}
void Print(string s)
{
Console.WriteLine(s);
}
}
// TODO: trying to figure out how to reference my created methods in a seperate space...I think?
//class TestArea
//{
// List<string> catFact;
// Console.WriteLine(catFact);
//}
#endregion
}