+ 1

what defrant between static variable and non static variable

21st Jul 2016, 9:08 PM
Maichael Nasr
Maichael Nasr - avatar
2 Answers
+ 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); } } }
21st Jul 2016, 11:45 PM
Idril
Idril - avatar
0
Keyword
17th Aug 2016, 10:37 AM
Yashwanth Lingala
Yashwanth Lingala - avatar