Reputation: 45
I am trying to make simple add method. what I've figured out so far is that I have to declare static when I create method outside of main function and I can't use modifier in Main method when I create method. but I need more precise insight why? My code is below.
1.Use Static
static void Main(string[] args)
{
int a = 100;
int b = 200;
int add = Add(a, b);
int sub = Subtract(a, b);
System.Console.WriteLine($"This is Practice3: {add}");
System.Console.WriteLine($"This is Practice3: {sub}");
}
static public int Add(int k, int q)
{
return k + q;
}
static public int Subtract(int y, int z)
{
return y - z;
}
in this code, the reason using static for Add, Subtract method is to use them in Main method without instantiating them? Am I right? that means each method are Object type?
2.
static void Main(string[] args)
{
int a = 100;
int b = 200;
int add = Add(a, b);
int sub = Subtract(a, b);
int Add(int k, int q)
{
return k + q;
}
int Subtract(int y, int z)
{
return y + z;
}
System.Console.WriteLine($"This is Practice3: {add}");
System.Console.WriteLine($"This is Practice3: {sub}");
}
However, in 2nd code, V.S doesn't allow me to use modifier(like public, private...). It just does allow me to use return type of method. I don't understand why.
does anybody give me some clues? Thank you in advance!!
Upvotes: 1
Views: 150
Reputation: 828
Visual Studio doesn't allow me to use a modifier(like public, private...). It just does allow me to use the return type of method. I don't understand why.
It's because you are trying to create methods inside the main function.
You can create methods with modifiers only from the outside of the function. You do not need to add a modifier because its already in the function.
Compiler counts that function as a local function.
Upvotes: 1
Reputation: 1685
Couple of points here -
- The reason using static for Add, Subtract method is to use them in Main method without instantiating them? Am I right? that means each method are Object type?
Yes
. you can access them without instantiating the Program
object, That is the main feature of the static methods in C#. Suppose if you want to access these methods outside of Program Class then you could use them something like below -
class TestClass
{
public void printAdd()
{
int addResult = Program.Add(3, 3);
Console.WriteLine(addResult);
Console.Read();
}
}
V.S doesn't allow me to use modifier(like public, private...). It just does allow me to use return type of method. I don't understand why.
In C# -
Access modifiers are keywords used to specify the declared accessibility of a member or a type.
So with the very definition of the access modifiers it should be applied to members. In case of your code Main Method
is a member of a class(Main). Similar can be applied to add and subtract methods in the 1st code snippet and you can apply modifiers there.
So you cannot apply the access modifiers to add and subtracts methods in 2nd code snippet. They are local variables declared inside a method and not members of a type Program. You may have to see the below answer for more information on member variables
and method variables
-
Difference between Class variable, Member variable, and Local variable, Global Variable
And below link for access rules on access modifiers
for different type members -
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/accessibility-levels
Upvotes: 2