Reputation: 421
Reading and messing around to understand static classes and static methods and their differences with non static methods and their usage which i still don't get except maybe for the Main method which must not be made an instance(object) of a class.
Why if i try to use:
using static System.ApplicationId;
public class Program
{
static void Main()
{
Copy(SOMETHING)// Copy method doesn't exist
}
}
Then try to use a method from ApplicationId like Copy();
The IDE? can't find the method?
Doing the same with:
using static System.Console;
public class Program
{
static void Main()
{
Writeline("Hello"); // OK
}
}
Then try to find a method from Console like WriteLine();
It can find it and i can use it.
Why does that happen?
My understanding is That those are both static members? Isn't that the reason why i cant make instances of those 2 classes? Yet i can use the methods in the second example but not in the first one since it seems that it doesn't let me (error: copy() doesn't exist in the current context...).
Upvotes: 0
Views: 104
Reputation: 2379
The short answer is that you wouldn't use the static
keyword in a using
statement for non-static namespaces like System.ApplicationId
To use methods on a non-static (instance) class, you have to first make an instance of it using new
.
The tl;dr part follows
To avoid having to write out the namespace prefix System.
every time you want to refer to ApplicationId
, you can add
using System;
and then in the class, you can get to the someMethod() method with:
new ApplicationId().someMethod();
Now let's talk about static:
Before C# version 6, you just couldn't use a using
statement on a static class, so to get access to a static method like
System.Console.WriteLine()
you'd first add the non-static parent (System
) with
using System
and then refer to the static method using the namespace
Console.WriteLine
With the using static
syntax, you add the 'Console.
' part of the namespace once per class on the using
statement like so:
`using static System.Console;`
and then you can use WriteLine()
instead of Console.WriteLine
. I have to guess that the WriteLine()
method has got to be the most common use case for this functionality.
If you check out https://msdn.microsoft.com/en-us/library/system.console%28v=vs.110%29.aspx it shows that System.Console is a static class, so using static
is appropriate there.
However, System.ApplicationId is non-static (see https://msdn.microsoft.com/en-us/library/system.applicationid%28v=vs.110%29.aspx) so you can't use using static
.
Upvotes: 3
Reputation: 271810
The answer is simple, the methods in ApplicationId
are not marked as static
.
Let's see the difference between static and instance methods:
Static method:
Console.WriteLine();
******* *********
^ ^
class method
name name
Instance methods:
Random aRandomObject = new Random();
aRandomObject.Next();
************* ****
^ ^
name of an method
instance of name
Random
As you can see here, you need an instance of that class in order to use non-static methods. But for static methods, you don't need an instance.
The using static
directive allows you to omit the class name when you call a static method.
"But why doesn't it allow us to call instance methods like this?" you asked. As I have said above, you need an instance to call instance methods. If you only write the method name to call the method, how can the compiler know what instance do you want to call it on?
Upvotes: 2
Reputation: 2797
ApplocationId is not a static class, so you need to create an instance of it.
Upvotes: 0