C# Tutorial
C# examples, c# properties (get and set), properties and encapsulation.
Before we start to explain properties, you should have a basic understanding of " Encapsulation ".
- declare fields/variables as private
- provide public get and set methods, through properties , to access and update the value of a private field
You learned from the previous chapter that private variables can only be accessed within the same class (an outside class has no access to it). However, sometimes we need to access them - and it can be done with properties.
A property is like a combination of a variable and a method, and it has two methods: a get and a set method:

Example explained
The Name property is associated with the name field. It is a good practice to use the same name for both the property and the private field, but with an uppercase first letter.
The get method returns the value of the variable name .
The set method assigns a value to the name variable. The value keyword represents the value we assign to the property.
If you don't fully understand it, take a look at the example below.
Now we can use the Name property to access and update the private field of the Person class:
The output will be:
Try it Yourself »
Advertisement
Automatic Properties (Short Hand)
C# also provides a way to use short-hand / automatic properties, where you do not have to define the field for the property, and you only have to write get; and set; inside the property.
The following example will produce the same result as the example above. The only difference is that there is less code:
Using automatic properties:
Why Encapsulation?
- Better control of class members (reduce the possibility of yourself (or others) to mess up the code)
- Fields can be made read-only (if you only use the get method), or write-only (if you only use the set method)
- Flexible: the programmer can change one part of the code without affecting other parts
- Increased security of data

COLOR PICKER

Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Recently viewed tickets
CODE SNIPPETS
All the Code Snippets and Samples you need
How to set a Property Value by Name in C# and VB.NET

By Administrator
To set a Property Value by Name in C# and VB.NET you can use the following snippet.
Sample VB.NET
Related post, how to export gridview data to excel using devexpress xtragrid, how to catch specific ms-sql sqlexceptions in c# and vb.net, how to extract a password protected zip file using dotnetzip in c# and vb.net, 2 thought on “how to set a property value by name in c# and vb.net”.
RT @CodeSnippetsNET: How to set a Property Value by Name in C# and #VB .NET http://t.co/1evOj3lbnf #csharp #dotnet #dev
Leave a Reply Cancel reply
You must be logged in to post a comment.
- Trending Categories

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
How to set a property value by reflection in C#?
The System. Reflection namespace contains classes that allow you to obtain information about the application and to dynamically add types, values, and objects to the application.
Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in the System. Reflection namespace.
Reflection allows view attribute information at runtime.
Reflection allows examining various types in an assembly and instantiate these types.
Reflection allows late binding to methods and properties.
Reflection allows creating new types at runtime and then performs some tasks using those types.
GetProperty(String)
Searches for the public property with the specified name.
GetType(String, Boolean)
Gets the Type object with the specified name in the assembly instance and optionally throws an exception if the type is not found.
SetValue(Object, Object)
Sets the property value of a specified object.

- Related Articles
- How to set a property having different datatype with a string value using reflection in C#?
- How to fetch a property value dynamically in C#?
- How to set align-self property to its default value in CSS?
- How to delete an element from the Set by passing its value in C++
- Reflection in C#
- How to Invoke Method by Name in Java Dynamically Using Reflection?
- How do you give a C# Auto-Property a default value?
- How to set a value to a file input in HTML?
- How to set result of a java expression in a property in JSP?
- How to set Octet value in JavaTuples
- How to display methods and properties using reflection in C#?
- Mirror Reflection in C++
- Line Reflection in C++
- How to set selected item of Spinner by value instead of by position on Android?
- Set a certain value first with MySQL ORDER BY?
Kickstart Your Career
Get certified by completing the course

IMAGES
VIDEO
COMMENTS
In real property transactions, a deed of assignment is a legal document that transfers the interest of the owner of that interest to the person to whom it is assigned, the assignee. When ownership is transferred, the deed of assignment show...
A Notice of Assignment is the transfer of one’s property or rights to another individual or business. Depending on the type of assignment involved, the notice does not necessarily have to be in writing, but a contract outlining the terms of...
When it comes to selling your property, you want to get the best price possible. To do this, you need to make sure that your property is in the best condition it can be in. Here are some tips to help you increase the market value of your pr...
Type _type = Type.GetType("Namespace.AnotherNamespace.ClassName"); PropertyInfo _propertyInfo = _type.GetProperty("Field1"); _propertyInfo.
The new property value. Exceptions. ArgumentException. The property's set accessor is not found. -or-. value cannot be converted
The set method assigns a value to the name variable. The value keyword represents the value we assign to the property. If you don't fully understand it, take a
In the previous code segment, if you don't assign a value to the Name property, it will return the value NA . The set accessor. The set accessor
A get property accessor is used to return the property value, and a set property accessor is used to assign a new value. An init property
How set property name/value dynamically?, How to set multiple property values with same name and counter value using reflection?
name must be used // to search for the property. In this example, the
Description: If I have a class called person with a property of FirstName. I can access the value like this: Person.FirstName = "Mike" Is th.
To set a Property Value by Name in C# and VB.NET you can use the following snippet. Sample C#. 1. 2. 3. 4. 5. 6. 7.
GetProperty(String). Searches for the public property with the specified name. · GetType(String, Boolean). Gets the Type object with the
... property by reflection with a string value, Accessing C# property name or attributes. ... Net - C# - How to assign a property value to a object