---
title: "How to get a private property name with value"  
description: "How to get a private property name with value"  
author: "Samuel Fernandes"  
published: 2013-12-16  
updated: 2013-12-16  
canonical: https://www.mindstick.com/forum/1777/how-to-get-a-private-property-name-with-value  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to get a private property name with value

Is there anyway I can get my [private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) of a [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) from another class? Here what I have tried.

```
class Program{    static void Main(string[] args)    {        Sample aSample = new Sample();        //Is there anyway to access that Name private property here?    }}class Sample{    private string Name { get; set; }}
```

## Replies

### Reply by Pravesh Singh

Here I have tried with Reflection namespace and PropertyInfo class.

Please try it if you want property name with it's value. But in this case your property should be public. It shows all property names and their values.

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;namespace PropertiesImpConsoleApp{  class Student  {     //Declare variables     string firstname;     string lastname;    //Define property for the variables     public string FirstName     {         get         {             return firstname;         }         set         {             firstname = value;         }     }     public string LastName     {         get         {             return lastname;         }         set         {             lastname = value;         }     }  }  class MyMain  {      public static void Main(string[] args)      {          Student aStudent = new Student();          Console.WriteLine("Enter First Name");          aStudent.FirstName = Console.ReadLine();          Console.WriteLine("Enter LastName");          aStudent.LastName = Console.ReadLine();         //And to get the properties names you can do like this          Dictionary<string, string> aDictionary = new Dictionary<string, string>();          PropertyInfo[] allproperties = aStudent.GetType().GetProperties().ToArray();          foreach (var aProp in allproperties)          {              aDictionary.Add(aProp.Name, aProp.GetValue(aStudent, null).ToString());          }          foreach (KeyValuePair<string, string> pair in aDictionary)          {              Console.WriteLine("{0}, {1}",              pair.Key,              pair.Value);          }          Console.ReadLine();      }   }}
```


---

Original Source: https://www.mindstick.com/forum/1777/how-to-get-a-private-property-name-with-value

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
