How to use static constructor in struct with C#

1 Answer

0 votes
using System;
 
struct Student {
    public int id;
    public string name;
 
    static Student() {
        Console.WriteLine("struct Student");
    }
 
    public Student(int _id, string _name) {
        id = _id;
        name = _name;
    }
}
 
class Program
{
    static void Main(string[] args)
    {
      Student s = new Student(34678, "Emma");
       
      Console.WriteLine(s.id);
      Console.WriteLine(s.name);
    }
}
  
  
  
/*
run:
  
struct Student
34678
Emma
  
*/

 



answered Dec 15, 2020 by avibootz
edited Dec 15, 2020 by avibootz

Related questions

1 answer 197 views
1 answer 119 views
4 answers 280 views
1 answer 99 views
1 answer 151 views
1 answer 128 views
1 answer 210 views
...