Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count class properties in C#?

How to count this DataSources class properties? Answer should be '3'

public class DataSources
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Phone { get; set; }
    }
like image 892
Umar Asif Avatar asked Sep 17 '25 01:09

Umar Asif


1 Answers

You can investigate type metadata with classes found in System.Reflection namespace. In your case TypeInfo-class is one which help you when getting information about properties.

using System.Linq;

typeof(DataSources).GetProperties().Count();

Or

typeof(DataSources).GetProperties().Length;
like image 126
Risto M Avatar answered Sep 19 '25 13:09

Risto M