C
C#3w ago
ZcAul

how do i use classes?

i only just started learning c# but i just cannot grasp how to use classes, objects and things to do with that. The only other language i have some experience in is python, so im also quite new to using oops as a whole. Is there any advice on how i should grasp it, or how to practice using them etc?
42 Replies
bornacheck
bornacheck3w ago
Do you know classes in Python or do you not know these concepts yet?
ZcAul
ZcAulOP3w ago
i know the concepts but actually using them without getting some dumb syntax error is very rare lol
bornacheck
bornacheck3w ago
Ok so can you please give a snippet of your code that generated the syntax error?
ZcAul
ZcAulOP3w ago
umm ok hold on ill just try to make smth basic like very basic because i dont really have anything on me :) i hope thats alright
bornacheck
bornacheck3w ago
Ok that's absolutely fine
mtreit
mtreit3w ago
Have you gone through any kind of tutorial like this one here that introduces classes and what they are all about? https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/classes
Classes and objects tutorial - C#
Create your first C# program and explore object oriented concepts
ZcAul
ZcAulOP3w ago
i have but honestly i still struggle to apply it
ZcAul
ZcAulOP3w ago
for this what exactly is the error
No description
ZcAul
ZcAulOP3w ago
i can send the codes too if youd like
mtreit
mtreit3w ago
The fundamental idea behind a class is that you have data (in the form of fields and properties) and methods (functions that perform operations, typically based on the that data) and they are bound together into a single thing. You can then create different instances of those things (instance of a class being a very important concept) that can be accessed independently.
ZcAul
ZcAulOP3w ago
mhm i think i understand that bit i think i struggle more with actually applying that using the syntax of c#
bornacheck
bornacheck3w ago
Oh I figured out the error: your Main method should be in a class. It cannot be in a namespace Do you understand my wording?
mtreit
mtreit3w ago
If you can explain what part trips you up we can help clarify.
ZcAul
ZcAulOP3w ago
ohh ok i see i guess calling on an attribute by creating an instance like i wanna reference colour but i have to create a new objet and stuff in the above code
mtreit
mtreit3w ago
"Attribute" has a very specific meaning in C#...I am not sure you are using it to mean metadata...
bornacheck
bornacheck3w ago
I assume in this case you are not referring to Attribute classes but you are referring to class members. Right? In this case your Car class has two members
ZcAul
ZcAulOP3w ago
oh no sorry i mean members
bornacheck
bornacheck3w ago
I think they mean members because in Python class attributes are members
mtreit
mtreit3w ago
One issue might be that you did not specify an access modifier for your fields, so they will be private by default and not accessible to code outside the class. You should mark them (potentially) as public or internal so that you can access them from the code that creates an instance of the class.
ZcAul
ZcAulOP3w ago
ok i think i understand it now since i put the main inside the class i didnt know you had to do that :) oh sorry also can you use main() with its own definition if ykwim inside multiple classes or do you name its something different i assume main is somewhat special since you dont have to call it to run it it does it automatically sorry if that doesnt make any sense :(
mtreit
mtreit3w ago
Main is special.
ZcAul
ZcAulOP3w ago
ok :)
mtreit
mtreit3w ago
$mains
MODiX
MODiX3w ago
The possible signatures for Main are
public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }
public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }
public is not required (can be any accessibility). Top-level statements are compiled into a Main method and will use an appropriate signature depending on the body. https://docs.microsoft.com/en-US/dotnet/csharp/fundamentals/program-structure/main-command-line
Main() and command-line arguments - C#
Learn about Main() and command-line arguments. The 'Main' method is the entry point of an executable program.
ZcAul
ZcAulOP3w ago
ohh its for executable yeah that makes sense so the main line of code should fall inside main obviously ok i think i get it now tysm <3
mtreit
mtreit3w ago
Yes, it's what we call the "entry point" for your program - the loader when you execute the program will do some bootstrapping work and then invoke the entry point - in this case the Main function. So that's where your program starts executing.
ZcAul
ZcAulOP3w ago
ok thank you :)
bornacheck
bornacheck3w ago
@ZcAul does that solve your problem or do you need further elaboration?
ZcAul
ZcAulOP3w ago
it did ty :0 actually umm
ZcAul
ZcAulOP3w ago
No description
ZcAul
ZcAulOP3w ago
how do i call upon the edit list method when i try to it says it either needs a return type or doesnt exist within the current context
bornacheck
bornacheck3w ago
ok so first of all, WHERE are you calling it from?
ZcAul
ZcAulOP3w ago
actually i fixed it its fine dw :)
bornacheck
bornacheck3w ago
ok np then
ZcAul
ZcAulOP3w ago
just had to add this :)
No description
bornacheck
bornacheck3w ago
yep
ZcAul
ZcAulOP3w ago
why is system.string[] being printed?
No description
Angius
Angius3w ago
The default .ToString() overload for any object is to print the name of the class Arrays (and lists and other collections) don't have a nice .ToString() method that would return, say, comma-separated values So it's something you need to handle yourself. Either by printing in a loop, serializing to a JSON string, or using string.Join()
ZcAul
ZcAulOP3w ago
ok :)
bornacheck
bornacheck3w ago
when you print an array, or really any object in C# using Console.WriteLine every single object has a ToString method, and by default it just says literally what it is. in this case if you want to print all the elements, you have to do it one by one
Fayoka
Fayoka3w ago
This isn't really accurate, but for a beginner I think it's a way to think about it till you start doing some more advanced stuff. But a class is everything, it can either be a 'Car', but it can also be a Client you set up for a database connection, a class can be something that holds Utilities to use throughout your program, or a task you want to execute whatever you want to do, start from a class. Here in this class you can define properties; you set those with getters and setters, while one lets you get the value from another class, the other sets it from another class. When you have this class, you can initialize it with the 'new' keyword, this creates a object of the class. There is so much more to it, but understanding this, is what got me to play around with it. But maybe it's bad information
bornacheck
bornacheck3w ago
Yeah a few things i want to point out based on that: classes are reference types, that means if you pass a class to a method (class function) then it will be able to modify it even without returning it:
public class Program {
public class Car
{
public string Color { get; set; } = "red";
public string Type { get; set; } = "awd";
}
static void MakeCarFourWheelDrive(Car car) => car.Type = "4wd";
static void Main()
{
Car car = new(); // creates a car
Console.WriteLine(car.Type); // awd
MakeCarFourWheelDrive(car); // despite the method not returning anything, it will still modify the car
Console.WriteLine(car.Type); // 4wd
}
}
public class Program {
public class Car
{
public string Color { get; set; } = "red";
public string Type { get; set; } = "awd";
}
static void MakeCarFourWheelDrive(Car car) => car.Type = "4wd";
static void Main()
{
Car car = new(); // creates a car
Console.WriteLine(car.Type); // awd
MakeCarFourWheelDrive(car); // despite the method not returning anything, it will still modify the car
Console.WriteLine(car.Type); // 4wd
}
}
Structs which you may or may not know about, are value types, that means if you pass the struct to a method then it will not be able to modify it unless it returns it. Yes properties are great for these things, and you can use { get; set; } to get automatic properties. But properties are basically like methods, get and set are their own methods. The rest of the information is pretty good but i wanted to give some clarification on some things that may be important

Did you find this page helpful?