Properly understanding Public Static
^^^ title, I've got a test coming up, I avoided understanding this last test and skipped the optional questions with them ... I think I'd rather try to understand them now.
And so, For example;
What purpose does "double x" serve us here?
Or, better yet. if it were to be broken down by each syntax, used & explained.
Sorry if it sounds like a dumb question, That's why I came here to ask!
P.S if the code is missing something, I apologize ... our teacher makes us learn on pen and paper so I don't always know how to properly write stuff on a console app.
101 Replies
another example would be.
the ; at the end of each if line essentially terminates your if condition there
so u dont want it there for the ifs
oh right yeah, sorry. aint paying much attention 😭
a bit tired since i've been studying for awhile
another thing you're missing in your code is the return type
another example that lacks the return type
Methods - C# Guide - C#
Overview of methods, method parameters, and method return values
GeeksforGeeks
C# | Methods - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
So for example if i returned X here, id return X in the form of int?
or am i getting this wrong
anything u return from that method must be an int
The method return type denotes what the method returns
There's no "in the form of"
that is what the return type defines
The method says it returns an
int
, so you must return an int
aahhh, okay.
and one more question i think.
when we saayy:
the x,y,z are the parameters INSIDE the "ex1" method? which is also a public method?
They are the parameters of the
ex1
method, yesagain u're missing the return type
It is also public, yes
OH yeah. thank you
also dont make static an habit, for the sake of explaining its fine but static everything is no bueno
unfortunately by my teacher static is everything right now
Oof
his way of teaching is kinda wack
Teachers, man
Worst programmers there are lmao
as long as you understand its not its fine
u may use it a lot in console apps
until u learn OOP etc
like i dont even know how to use public on a console app bc it just errors out for me
he teaches very like
show an example of how you're using it
poorly 😭
Uh, no, making a method public should not be causing any errors
I would assume you're trying to declare a local variable with public
and thus causing the error
Or a local function
My suspicion as well
i.e.:
Yeah
Local variables are local
because access modifiers have their own place in the scope
They cannot be public, private, protected, or internal
or when i use SOME thing i may be mistaken about if its that one, but the console app suggests a
"using class
sm sm console"
i dont really remember, and even applying it then it doesnt work.
i havent learned classes yet
Access modifiers talk about the member's accessibility from outside of the class
well, he hasnt taught us them *
A method is not a class
So access modifiers on elements local to the method make no sense
and we dont even touch computers yet in his class while we're surrounded by them, really frustrating.
this for example?
Uuhm, nope i havent a clue what variety is yet.
I assume imports, so
using System.Foo.Bar.Baz;
Ah, well i must be mistaken then but thank you.
I would recommend $helloworld
The teacher might be a waste of oxygen, but it doesn't mean you can't learn on your own
https://learn.microsoft.com/en-us/shows/csharp-fundamentals-for-absolute-beginners/
or if u prefer videos, personally watch those for the sake of suggesting something to people.
they are short but concise videos about specific parts and the person talking is good at explaining what is going on
ahah, a bit yeah.
i do learn on my own sometimes! i used to use sololearn back when we learned python, its HORRIBLE for c# though (well, in my experience .. its really confusing and vague.)
i had a head start and actually scored a pefect 100 on my first test, i assume Microsoft is a good source? ive come across a few pages of theirs when i was trying to figure out something and it didnt make much sense to me, dont think its their fault though.
was awhile ago though so i dont remember what it really was.
i will check that out aswell, thank you!
Well, yes, Microsoft made C# and .NET
If there was ever a good resource on learning those, it would be Microsoft lol
just keep in mind, learning the language takes a lot of practice and repetition, so u learn 1 thing, open a console project, practice using what u have learned
if u dont understand it, u ask questions
the important is to be able to digest and use what u learned
otherwise it will be as if u only looked thru it to soon forget and when u actually need it u no longer know
the idea is, to not take too much at once, u learned about string manipulation? then go practice it a bit, see it working, see what u dont understand etc
Oh, darn. honestly stupid to admit, but i didnt know they made it 😭
I dont really know any knowledge about c# outside of the actual coding experience itself
As a side note, you say you haven't used computers. I assume you're writing code on paper, 1997-style?
If so, I highly recommend you do use the computer at home, and do try to run your code
mhm! thats what i always try to do, and when it fails on my own i usually just ask other people on here, really love asking because theyre really helpful. :)
yep, in class. although at home ive already got C# fitted on vsc community
use it all the time.
VSC or VS Community?
Two different things
uuuah.
the purple one 😭
VS, then
Visual Studio 2022 Community
right , yeah
mixed them up
yeah VS 2022 is the good one to get started with
alright, yeah. seems ive garbled public static a bit in my brain previously and failed to understand it, i'll attempt to solve in a bit after i eat some food. but thank you all so far. ^^
this is i THINK what i was previously talking about.
Where is this method?
It's inside of another one, isn't it?
it's just, out in the open really. it's not inside anything i atleast don't think so..?
Show the whole file
Ah
This is a top-level statements file
It's treated as the inside of the
Main
method, to put it simply
So this is treated as a local method
And, as we already know, it makes no sense for local members to have access modifiershow would you explain local members exactly? are they basically "local code" in a nutshell? compared to public and private codes?
This code compiles to, more or less
Local to a method
So, being inside of the method
OHH okay.
Class members can have access modifiers on their members, since they can be accessed from outside of that class and we need to control how
so, what we've done here is created the class "program" to subject the method to it?
well, i don't knoww if i worded it correctly exactly as i have barely any idea how any of this works.
(due to studying on paper.)
Compiler takes the contents of the file and wraps them in a class and a
Main
methodso it's, a hierachy?
$structure
Yes
Namespace -> class -> method
That'd be the basic of it
Methods can have local methods
Classes can have inner classes
But for the most part, it's namespace -> class -> method
Or, more aptly, namespace -> class -> class member
wow that's really. interesting how deep it can go
i think i should pick this up on my own sometime, get ahead of whenever my teacher decides to teach it.
what would you call this entire.. thing?
structure types?
Oh , no that's something else ..
Not sure it has a name like that
It's just... the program's structure?
It consists of classes, classes can have members, etc
oh right yeah. i mistook it for something else.
found rsources from microsoft about it.
Haang on, is this one supposed to work with a top-level statement file?
Using this code will make the file no longer a top-level statement file
Since it has a class now, and one of the accepted entrypoint methods
$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.
it seems to drop the same error again, that's why i had asked.
Ah
Right
This was what your code would compile into
And thus, would throw an error
But now it's clearer to see that
EX1
is local to Main
A proper, working example would be either
or
First one uses a proper local method (which is unorthodox but not unheard of)
The other has the method as a class memberBare with me, but i am SO confused right now.
What about?
One second.
So,
is compiled by the computer, and read by it as this?
and.. i understand you've already given corrected versions of this, but basically. can you explain why it wouldn't work? just genuinely confused over it.
Yes, that's what it compiles to
And it wouldn't work, because
EX1
is a method local to the Main
method
Class members — like Main
— need the access modifier to control how they can be accessed from the outside of the class. For example
So, things that are inside of a class can be visible to the outside, invisible to the outside, or even visible but only to the class's children
Methods, meanwhile, keep everything local to them
There's mo local method that can be public. There's no local method that can be private.
There is no outside access to method's local code
And thus, an access modifier cannot even be used for them
Same reason why local methods cannot be static
, since that also controls how the member is accessed from the outside, whether it's by class name or class instance.
Local stuff is... local, it cannot be accessed, so static
is useless anyway
Thus, method's code — be it variables or local methods — cannot have either access modifiers or be static
oh my god, that is SO mind boggling to process, but i think i get it.
Think of yourself as a method
You live in a house (class)
And you control access to the things in your house
By putting a padlock on the fridge, but leaving the TV freely accessible
mhm.
And punching anybody who wants to access you
But your lungs, heart, and spleen are inside of you
Local to you
No need to put a padlock on your pancreas
People can't access them anyway
that's such a funny explanation, made me giggle a bit but it really helped.
thank you.
so,
this, works. no errors, is it FINE ?
i don't know how to assign a value to the "double x" so im not sure if the output is correct.
EX1
is a method, so you need to call it for it to run
For example,
Waaittt, whaaat?
it's like actual magic, how does that work?!
and doesn't the order of them matter? (i ask this since it just.. well worked flawlessly.
I'm confused specifically onn..
i understand result is a variable, and yes. we called the method.
but, what exactly happens..?
No, order doesn't matter
What happens, is that you pass
67.99
to the method, it gets placed in the x
parameter
Then the method does it's thing
And return
s a value
That return
ed value can be saved in a variableoh my god that's actually so like, fascinating.
It's like a blender
You input a banana and milk
Blender does its thing
You pour out a banana smoothie
and out comes the smoothie.
Yep
that's so gahhh i don't know, it just fascinates me
i learned ALOT. i think that's all i've had to ask.
i can't thank you enough.
Well, I can. thank you so much bwahaha.
Anytime 👍
Hope you have aa good day, whatever time it is for you.
<3
I'll close this post now.