-
Notifications
You must be signed in to change notification settings - Fork 0
/
Counter.cs
59 lines (53 loc) · 1.53 KB
/
Counter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Text;
namespace Arrays
{
public class Counter
{
//notice the privte constructor. A private constructor is a special instance constructor.
//It is generally used in classes that contain static members only.
//If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class.
private Counter()
{
}
public static int currentCount;
public static int IncrementCount()
{
return ++currentCount;
}
}
public class TestCounter
{
//static void Main()
//{
// //Counter ctr = new Counter();
// Counter.currentCount = 10;
// Counter.IncrementCount();
// Console.WriteLine("New count: {0}", Counter.currentCount);
//}
}
//public class Student
//{
// static Student()
// {
// // initialize static members only.
// }
// // other methods here.
//}
// Example2
public class MyStaticClass
{
static MyStaticClass()
{
Console.WriteLine("Static class is initialized");
}
public static void MyMethod(string name)
{
Console.WriteLine("Static class is initialized " + name);
Console.ReadLine();
}
}
//MyStaticClass.MyMethod("Obi") ; //method call
//constructor will be called automatically
}