+ 1
what defrant between static variable and non static variable
2 Respostas
+ 3
This should answer your question.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
StaticVersusNonStatic first = new StaticVersusNonStatic(); // 1
first.Val = 4;
StaticVersusNonStatic second = new StaticVersusNonStatic(); // 2
// second.Counter = 10; // ERROR, but
StaticVersusNonStatic.Counter = 10; // works. You have to access this static variable by the name of the type.
StaticVersusNonStatic third = new StaticVersusNonStatic(); // 11
}
}
public class StaticVersusNonStatic
{
public static int Counter;
public int Val = 3;
public StaticVersusNonStatic()
{
Counter++;
Console.WriteLine("Created with StaticVersusNonStatic.Counter = {0}", Counter);
}
}
}
0
Keyword