krixsick
❔ C# Inheritance Topic Question Mostly about base constructors
Hello! I just had some question regarding C# inheritance. So far, I have a few questions regarding on how inheritance works especially, the base constructors. I understand that inheritance is basically making a class a sub-class or child of a base class or parent. Because of this, the sub/child class can access the methods provided in the parent class. For base constructors, I can't really visualize on scenarios of using them effectively and how they function 100%. So far my questions are this:
1. Do the fields inside both classes get shared when a class inherits from another class
2. Using :base in the derived class' constructor makes the base class constructor called first, but why do we want this to happen? I understand that both constructors can take in different parameters and depending on those parameters, the fields will store that information type. But why do we have to initialize the base class if we initialize the derived class (child class)
Thanks!
22 replies
❔ How to display All the integers in a list concatenated with a string
Hello! I was wondering if there was a possible way of cancatenating elements inside a list (that are integers) and combining it inside a console.WriteLine without having to do list.[index]. I was thinking of a for loop, but couldn't get it to print it all out in one line.
14 replies
✅ Complier Error CS0120, Can't understand why I can't call this private field
I just had a quesiton about my code here: In my logic method, the moveOptions has a Compiler Error CS0120, saying that I have to create an instance of my class. But if I were to create a class sheet, and put the moveOptions under the class, the other methods should be able to use it i think, (could be wrong), is it because of the static? What does the static do because in another class file, they don't use static. I know this code shouldn't work right now, but I just can't understand why I can't use the private field in the logic method of this class
38 replies
❔ ✅ Lists vs arrays vs ArrayLists and jagged arrays and dictionaries vs hashtables
Hello! I'm currently still learning more about collections in C# and I came across about non-generic and generic. I think I understand the difference but correct me if I'm wrong is that generic collections are like specialized containers that can hold a collection of objects, but they are designed to work with a specific type of objects. On the other hand, non-generic are like containers that can hold different types of objects. They are not specific about the type of objects they can store. Because of this, ArrayList, Hashtable, and SortedList are considered as non-generic collections and Arrays, dictionaries, Lists, Hashsets and other stuff are considered generic.
Now I'm still new to learning all of this so it probably isn't smart of me to find the difference between all of these and learn each one step by step. But currently some questions I have is
1. how dictionaries are considered a generic collection since can't they store strings and integers (my guess because python dictionaries can store ints and strings)
2. The difference between Arrays, Lists, and an ArrayList. I feel like lists basically outclass an array but the only difference I kinda see is when you initialize them. For an array if you know the amount of entries you can state that and create an array that can only hold like 5 entries. (int [] grades = new int[5];). But im learning arraylists right now and they seem very similar to lists.
3. Are jagged arrays useful? So far I dont think my skills are good enough to use jagged arrays efficiently so I was wondering if they're actually beneficial. Since I am not good at C# lol
4. I know the difference between hashtables and dictionaries is that one is generic and non-generic, but im just confused on how they both aren't considered non-generic
36 replies
❔ ✅ Basic Tic Tac Toe Checker that uses a 2-D Array
The question says this:
This time, you have to write only a checker for the game. It will be a method that takes a 2D array and returns a boolean. If there is a winner, it returns true otherwise false.
We use "X" and "O" as signs of our players. The empty places will be filled with numbers from 1 to 9.
You have to check 3 types of cases:
horizontal;
vertical;
diagonal;
The answer to the code is:
My answer to the code is this:
73 replies
❔ C# Error with a while loop (course work)
Hello! This i my code currently. The main issue that arises is that the code will still print out -9 for some reason. I'm not too sure on why it does that because I have a module function that checks whether a number is divisible by 3 or not. I was told not to change the "if (i == 11).
{
Console.WriteLine(" FINAL BREAK REACHED! This should not happen!");
break;
}
Console.WriteLine(i++);"
part but to instead add code inside the while function to do that. My goal is for the loop should skip all divisible by 3 values and stop running when i = 10. So far it achieves this result for the other numbers expect for -9
namespace Challenge_LoopsOneAverage
{
internal class Program
{
static void Main(string[] args)
{
int i = -10;
while (true)
{
if (i % 3 == 0)
{
i++;
continue;
}
else if (i == 10)
{
break;
}
Console.WriteLine(i++);
if (i == 11)
{
Console.WriteLine(" FINAL BREAK REACHED! This should not happen!");
break;
}
Console.WriteLine(i++);
}
}
}
}
12 replies