Properties in C#. Net

InfoPath alternatives for form designing SharePoint

Properties of an object are the characteristics the object has. Consider a Person object. The
most common properties i.e the characteristics does a Person have are:

First name

Last name

Date of birth

You define a property in a class with the Property keyword (in VB.NET) or with a property header similar to a method in C#. In both languages, you use a Get block (get in C#) and a Set block (set in C#) to define the so-called getters and setters of the property. The getter is accessed when an object is asked for the value of a specific property, and the setter is used to assign a value to the property. You can also check generics here.

Properties only provide access to underlying data stored in the object; they don’t contain the actual data. To store the data, you need what is called a backing variable.consider the below example

VB.NET

Public Class Person
Private _firstName As String
Public Property FirstName() As String
Get
Return _firstName
End Get
Set(ByVal value As String)
_firstName = value
End Set
End Property
End Class

C#.NET
public class Person
{

private string _firstName;
public string FirstName
{

get { return _firstName; }
set { _firstName = value; }

}

}

The FirstName property has a backing variable called _firstName, LastName has one called _lastName, and so on.
The main reason for a property in a class is to encapsulate data. The idea is that a property enables you to control the data that is being assigned to it.

For simple properties that don’t need any data manipulation or validation, you can use so-called
automatic properties. Automatic properties used to be available in C# only, but starting with the
release of .NET 4, automatic properties are now available in VB.NET as well.Consider the below syntaxs.

VB.NET
Public Property DateOfBirth As DateTime

C#
public DateTime DateOfBirth { get; set; }


The Visual Basic implementation of automatic properties has one advantage over the C# version: you can declare the property and give it a value in one shot like below

VB.NET
Public Property myDate As DateTime = DateTime.Now


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →

Leave a Reply