Professional Documents
Culture Documents
Staticka Klasa
Staticka Klasa
Сврха неке статичке класе је да служи као складиште услужних метода и поља.
Нека класа се може декларисати да је „статичка“, и ово подразумева да таква класа има само
статичке чланове тј. елементе. Сврха неке статичке класе је да служи као складиште услужних
метода и поља. Статичка класе не сме да садржи нестатичке методе и поља. Нема смисла
креирати објекат статичке класе, и ако покушате омпајлер ће вас упозорити на грешку. Статичка
класа може да има подразумевани конструктор под условом да је проглашен као „статиц“. Било
који други тип конструктора није дозвољен, и компајлер ће вас упозорити ако грешите.
......
...................}
1. static class classname
2. {
3. //static data members
4. //static methods
5. }
1. namespace StaticConstructorsDemo
2. {
3. Public static class MyCollege
4. {
5. //static fields
6. public static string CollegeName;
7. public static string Address;
8.
9. //static constructor
10. static MyCollege()
11. {
12. CollegeName = "ABC College of Technology";
13. Address = "Hyderabad";
14. }
15. }
16. class Program
17. {
18. static void Main(string[] args)
19. {
20. Console.WriteLine(MyCollege.CollegeName);
21. Console.WriteLine(MyCollege.Address);
22. Console.Read();
23. }
24. }
25. }
Non-static Methods
These methods can implement operations on non-static fields and properties
Static Members
If you use the "static" keyword for the declaration of a field / property or a method, then
it is called a "Static member". The main feature of a non-static member is that it will not
be bound with any object. It is individually accessible with the class name. In other
words, the static members are accessible directly, without even creating one object
also.
Static Methods
These methods can implement operations on static fields and properties only; and can‘t
access the non-static members.
Demo on static members
1. using System;
2. using System.Linq;
3. using System.Text;
4. using System.Collections.Generic;
5. namespace StaticConstructorsDemo
6. {
7. class Student
8. {
9. //non-statcic data members
10. public string StudentName;
11. public string Course;
12. public void SetStudentDetails(string StuName, string
Cou)
13. {
14. StudentName = StuName;
15. Course = Cou;
16. }
17. public void DisplayStudentDetails()
18. {
19. Console.WriteLine(StudentName + " - " + Course);
20. }
21.
22. //static data members
23. public static string CollegeName = "ABC College of T
echnology";
24. public static string CollegeAddress = "Hyderabad";
25.
26. //static methods
27. public static void DisplayCollegeDetails()
28. {
29. Console.WriteLine(CollegeName);
30. Console.WriteLine(CollegeAddress);
31. }
32. }
33. class Program
34. {
35. static void Main()
36. {
37. //access static members
38. Student.DisplayCollegeDetails();
39.
40. //acess non-static members
41. Console.WriteLine();
42. Student s1= new Student();
43. Student s2 = new Student();
44. s1.SetStudentDetails("Sarath","MCA");
45. s1.SetStudentDetails("Syam","MBA");
46. s1.DisplayStudentDetails();
47. s2.DisplayStudentDetails();
48. Console.Read();
49. }
50. }
51. }
Static Constructors
A static constructor is used to initialize the static data members, whereas the normal
constructor (non-static constructor) is used to initialize the non-static data members.
Syntax
1. static classname()
2. {
3. //some code
4. }
Rules
1. namespace StaticConstructorsDemo
2. {
3. class MyCollege
4. {
5. //static fields
6. public static string CollegeName;
7. public static string Address;
8.
9. //static constructor
10. static MyCollege()
11. {
12. CollegeName = "ABC College of Technology";
13. Address = "Hyderabad";
14. }
15. }
16. class Program
17. {
18. static void Main(string[] args)
19. {
20. Console.WriteLine(MyCollege.CollegeName);
21. Console.WriteLine(MyCollege.Address);
22. Console.Read();
23. }
24. }
25. }
https://www.c-sharpcorner.com/UploadFile/74ce7b/static-class-in-C-Sharp/