C
C#10mo ago
asianfxilure

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;
public static EX1(double x)
{
return x*x;
}
public static EX1(double x)
{
return x*x;
}
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
asianfxilure
asianfxilureOP10mo ago
another example would be.
public static EX2(int x);
{
if (x>0)
Console.WriteLine("1");
if (x<0)
Console.WriteLine("2");
if (x=0)
Console.WriteLine("3");
}
public static EX2(int x);
{
if (x>0)
Console.WriteLine("1");
if (x<0)
Console.WriteLine("2");
if (x=0)
Console.WriteLine("3");
}
leowest
leowest10mo ago
the ; at the end of each if line essentially terminates your if condition there so u dont want it there for the ifs
asianfxilure
asianfxilureOP10mo ago
oh right yeah, sorry. aint paying much attention 😭 a bit tired since i've been studying for awhile
leowest
leowest10mo ago
public -- The public keyword is an access modifier which is the most permissive access level.

static -- The static modifier is used to declare a static member which can be access from anywhere dependign on the outer class access level.

EX1 -- the method name

(double x) -- the parameters it can take, in your example it takes a parameter of the type double named x

{ -- brackets indicate the body of the method start

return x*x; -- what it returns in this case it returns double

} -- brackets indicate the body of the method ends
public -- The public keyword is an access modifier which is the most permissive access level.

static -- The static modifier is used to declare a static member which can be access from anywhere dependign on the outer class access level.

EX1 -- the method name

(double x) -- the parameters it can take, in your example it takes a parameter of the type double named x

{ -- brackets indicate the body of the method start

return x*x; -- what it returns in this case it returns double

} -- brackets indicate the body of the method ends
another thing you're missing in your code is the return type
public static double EX1(double x)
public static double EX1(double x)
another example that lacks the return type
leowest
leowest10mo ago
Methods - C# Guide - C#
Overview of methods, method parameters, and method return values
leowest
leowest10mo ago
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.
leowest
leowest10mo ago
No description
asianfxilure
asianfxilureOP10mo ago
So for example if i returned X here, id return X in the form of int? or am i getting this wrong
leowest
leowest10mo ago
anything u return from that method must be an int
Angius
Angius10mo ago
The method return type denotes what the method returns There's no "in the form of"
leowest
leowest10mo ago
that is what the return type defines
Angius
Angius10mo ago
The method says it returns an int, so you must return an int
asianfxilure
asianfxilureOP10mo ago
aahhh, okay. and one more question i think. when we saayy:
public static ex1(int x,int y,int z)
public static ex1(int x,int y,int z)
the x,y,z are the parameters INSIDE the "ex1" method? which is also a public method?
Angius
Angius10mo ago
They are the parameters of the ex1 method, yes
leowest
leowest10mo ago
again u're missing the return type
Angius
Angius10mo ago
It is also public, yes
asianfxilure
asianfxilureOP10mo ago
OH yeah. thank you
leowest
leowest10mo ago
also dont make static an habit, for the sake of explaining its fine but static everything is no bueno
asianfxilure
asianfxilureOP10mo ago
unfortunately by my teacher static is everything right now
Angius
Angius10mo ago
Oof
asianfxilure
asianfxilureOP10mo ago
his way of teaching is kinda wack
Angius
Angius10mo ago
Teachers, man Worst programmers there are lmao
leowest
leowest10mo ago
as long as you understand its not its fine u may use it a lot in console apps until u learn OOP etc
asianfxilure
asianfxilureOP10mo ago
like i dont even know how to use public on a console app bc it just errors out for me he teaches very like
leowest
leowest10mo ago
show an example of how you're using it
asianfxilure
asianfxilureOP10mo ago
poorly 😭
Angius
Angius10mo ago
Uh, no, making a method public should not be causing any errors
leowest
leowest10mo ago
I would assume you're trying to declare a local variable with public and thus causing the error
Angius
Angius10mo ago
Or a local function My suspicion as well
leowest
leowest10mo ago
i.e.:
static void Main()
{
// wrong and will not work but seems what u mentioned above.
public int number = 0;
}
static void Main()
{
// wrong and will not work but seems what u mentioned above.
public int number = 0;
}
Angius
Angius10mo ago
Yeah Local variables are local
leowest
leowest10mo ago
because access modifiers have their own place in the scope
Angius
Angius10mo ago
They cannot be public, private, protected, or internal
asianfxilure
asianfxilureOP10mo ago
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
Angius
Angius10mo ago
Access modifiers talk about the member's accessibility from outside of the class
asianfxilure
asianfxilureOP10mo ago
well, he hasnt taught us them *
Angius
Angius10mo ago
A method is not a class So access modifiers on elements local to the method make no sense
static void Main()
{
int number = 0;
}
static void Main()
{
int number = 0;
}
asianfxilure
asianfxilureOP10mo ago
and we dont even touch computers yet in his class while we're surrounded by them, really frustrating.
leowest
leowest10mo ago
static void Main()
{
using (var fs = new StreamWriter(...))
{

}
}
static void Main()
{
using (var fs = new StreamWriter(...))
{

}
}
this for example?
asianfxilure
asianfxilureOP10mo ago
Uuhm, nope i havent a clue what variety is yet.
Angius
Angius10mo ago
I assume imports, so using System.Foo.Bar.Baz;
asianfxilure
asianfxilureOP10mo ago
Ah, well i must be mistaken then but thank you.
Angius
Angius10mo ago
I would recommend $helloworld
Angius
Angius10mo ago
The teacher might be a waste of oxygen, but it doesn't mean you can't learn on your own
leowest
leowest10mo ago
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
asianfxilure
asianfxilureOP10mo ago
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!
Angius
Angius10mo ago
Well, yes, Microsoft made C# and .NET If there was ever a good resource on learning those, it would be Microsoft lol
leowest
leowest10mo ago
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
asianfxilure
asianfxilureOP10mo ago
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
Angius
Angius10mo ago
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
asianfxilure
asianfxilureOP10mo ago
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.
Angius
Angius10mo ago
VSC or VS Community? Two different things
asianfxilure
asianfxilureOP10mo ago
uuuah. the purple one 😭
Angius
Angius10mo ago
VS, then Visual Studio 2022 Community
asianfxilure
asianfxilureOP10mo ago
right , yeah mixed them up
leowest
leowest10mo ago
yeah VS 2022 is the good one to get started with
asianfxilure
asianfxilureOP10mo ago
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. ^^
asianfxilure
asianfxilureOP10mo ago
public static double EX1(double x)
{
return x*x;
}
public static double EX1(double x)
{
return x*x;
}
No description
asianfxilure
asianfxilureOP10mo ago
this is i THINK what i was previously talking about.
Angius
Angius10mo ago
Where is this method? It's inside of another one, isn't it?
asianfxilure
asianfxilureOP10mo ago
it's just, out in the open really. it's not inside anything i atleast don't think so..?
asianfxilure
asianfxilureOP10mo ago
No description
Angius
Angius10mo ago
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 modifiers
asianfxilure
asianfxilureOP10mo ago
how would you explain local members exactly? are they basically "local code" in a nutshell? compared to public and private codes?
Angius
Angius10mo ago
This code compiles to, more or less
public class Program
{
public static void Main()
{
public static double EX1(double x)
{
return x * x;
}
}
}
public class Program
{
public static void Main()
{
public static double EX1(double x)
{
return x * x;
}
}
}
Local to a method So, being inside of the method
asianfxilure
asianfxilureOP10mo ago
OHH okay.
Angius
Angius10mo ago
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
asianfxilure
asianfxilureOP10mo ago
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.)
Angius
Angius10mo ago
Compiler takes the contents of the file and wraps them in a class and a Main method
asianfxilure
asianfxilureOP10mo ago
so it's, a hierachy?
Angius
Angius10mo ago
$structure
MODiX
MODiX10mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
Angius
Angius10mo ago
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
asianfxilure
asianfxilureOP10mo ago
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 ..
Angius
Angius10mo ago
Not sure it has a name like that It's just... the program's structure? It consists of classes, classes can have members, etc
asianfxilure
asianfxilureOP10mo ago
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?
Angius
Angius10mo ago
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
MODiX
MODiX10mo 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.
asianfxilure
asianfxilureOP10mo ago
it seems to drop the same error again, that's why i had asked.
asianfxilure
asianfxilureOP10mo ago
No description
Angius
Angius10mo ago
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
public class Program
{
public static void Main()
{
double EX1(double x)
{
return x * x;
}
}
}
public class Program
{
public static void Main()
{
double EX1(double x)
{
return x * x;
}
}
}
or
public class Program
{
public static void Main()
{
}

public static double EX1(double x)
{
return x * x;
}
}
public class Program
{
public static void Main()
{
}

public static double EX1(double x)
{
return x * x;
}
}
First one uses a proper local method (which is unorthodox but not unheard of) The other has the method as a class member
asianfxilure
asianfxilureOP10mo ago
Bare with me, but i am SO confused right now.
Angius
Angius10mo ago
What about?
asianfxilure
asianfxilureOP10mo ago
One second. So,
public static double EX1(double x)
{
return x*x;
}
public static double EX1(double x)
{
return x*x;
}
is compiled by the computer, and read by it as this?
public class Program
{
public static void Main()
{
public static double EX1(double x)
{
return x * x;
}
}
}
public class Program
{
public static void Main()
{
public static double EX1(double x)
{
return x * x;
}
}
}
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.
Angius
Angius10mo ago
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
var foo = new Foo();

var one = foo.Bar; // allowed, since `Bar` is `public`
var two = foo.Baz; // error, since `Baz` is `private`

class Foo
{
public int Bar => 1;
private int Baz => 2;
}
var foo = new Foo();

var one = foo.Bar; // allowed, since `Bar` is `public`
var two = foo.Baz; // error, since `Baz` is `private`

class Foo
{
public int Bar => 1;
private int Baz => 2;
}
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
asianfxilure
asianfxilureOP10mo ago
oh my god, that is SO mind boggling to process, but i think i get it.
Angius
Angius10mo ago
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
asianfxilure
asianfxilureOP10mo ago
mhm.
Angius
Angius10mo ago
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
asianfxilure
asianfxilureOP10mo ago
that's such a funny explanation, made me giggle a bit but it really helped. thank you. so,
public class Program
{
public static void Main()
{

double EX1(double x)
{
return x * x;
}
}
}
public class Program
{
public static void Main()
{

double EX1(double x)
{
return x * x;
}
}
}
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.
Angius
Angius10mo ago
EX1 is a method, so you need to call it for it to run For example,
public class Program
{
public static void Main()
{
double result = EX1(67.99);
Console.WriteLine($"The result is {result}");

double EX1(double x)
{
return x * x;
}
}
}
public class Program
{
public static void Main()
{
double result = EX1(67.99);
Console.WriteLine($"The result is {result}");

double EX1(double x)
{
return x * x;
}
}
}
asianfxilure
asianfxilureOP10mo ago
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..
double result = EX1(67.99);
double result = EX1(67.99);
i understand result is a variable, and yes. we called the method. but, what exactly happens..?
Angius
Angius10mo ago
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 returns a value That returned value can be saved in a variable
asianfxilure
asianfxilureOP10mo ago
oh my god that's actually so like, fascinating.
Angius
Angius10mo ago
It's like a blender You input a banana and milk Blender does its thing You pour out a banana smoothie
asianfxilure
asianfxilureOP10mo ago
and out comes the smoothie.
Angius
Angius10mo ago
Yep
asianfxilure
asianfxilureOP10mo ago
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.
Angius
Angius10mo ago
Anytime 👍
asianfxilure
asianfxilureOP10mo ago
Hope you have aa good day, whatever time it is for you. <3 I'll close this post now.
Want results from more Discord servers?
Add your server