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:
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 »
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:
- 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
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
All the Code Snippets and Samples you need
How to set a Property Value by Name in C# and VB.NET
To set a Property Value by Name in C# and VB.NET you can use the following snippet.
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.
Searches for the public property with the specified name.
Gets the Type object with the specified name in the assembly instance and optionally throws an exception if the type is not found.
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