How to use static property in C#

2 Answers

0 votes
using System;

class Example
{
    static int _score;
    
    public static int Score {
        get {
            _score++;
            return _score;
        }
    }
}

class Program
{
    static void Main() {
        Console.WriteLine(Example.Score);
        Console.WriteLine(Example.Score);
        Console.WriteLine(Example.Score);
        Console.WriteLine(Example.Score);
        Console.WriteLine(Example.Score);
    }
}


 
/*
run:
    
1
2
3
4
5
  
*/

 



answered Aug 22, 2018 by avibootz
edited May 7, 2024 by avibootz
0 votes
using System;
 
class Student
{
    private static string _name;
 
    public static string Name {
        get {
            return _name;
        }
        set {
            _name = value;
        }
    }
}
 
class Program
{
    static void Main(string[] args)
    {
        Student.Name = "Tom";
         
        Console.WriteLine(Student.Name);
    }
}
 
 
 
/*
run:
 
Tom
 
*/

 



answered May 7, 2024 by avibootz

Related questions

1 answer 200 views
1 answer 202 views
1 answer 140 views
1 answer 123 views
123 views asked Apr 5, 2019 by avibootz
2 answers 201 views
201 views asked Aug 25, 2018 by avibootz
1 answer 225 views
...