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
Do you know classes in Python or do you not know these concepts yet?
i know the concepts but actually using them without getting some dumb syntax error is very rare lol
Ok so can you please give a snippet of your code that generated the syntax error?
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
Ok that's absolutely fine
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
i have but honestly i still struggle to apply it
for this what exactly is the error

i can send the codes too
if youd like
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.
mhm i think i understand that bit
i think i struggle more with actually applying that using the syntax of c#
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?
If you can explain what part trips you up we can help clarify.
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
"Attribute" has a very specific meaning in C#...I am not sure you are using it to mean metadata...
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
oh no sorry i mean members
I think they mean members because in Python class attributes are members
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.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 :(
Main is special.
ok
:)
$mains
The possible signatures for
Main
are 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-lineMain() and command-line arguments - C#
Learn about Main() and command-line arguments. The 'Main' method is the entry point of an executable program.
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
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.
ok thank you :)
@ZcAul does that solve your problem or do you need further elaboration?
it did ty :0
actually umm

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
ok so first of all, WHERE are you calling it from?
actually i fixed it its fine dw :)
ok np then
just had to add this :)

yep
why is system.string[] being printed?

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()
ok :)
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 oneThis 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
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:
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