properties types in Microsoft.Net Framework

how we create


trimurarisingh
Views: 1034 | Community Opinion: 2

Tags..  VB.NET  C#  .NET Framework

Bookmark this page..



Ask a New Question Go to Home

Community Opinion/Answers
 
trimurarisingh Said..

Properties are named members of classes, structs, and interfaces. They provide a flexible mechanism to read, write, or compute the values of private fields through accessors.

Properties are an extension of fields and are accessed using the same syntax. Unlike fields, properties do not designate storage locations. Instead, properties have accessors that read, write, or compute their values.

Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields. Properties can be used in any .Net Language like VB.Net, C# etc.





using System;
public class Employee
{
public static int numberOfEmployees;
private static int counter;
private string name;

// A read-write instance property:
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

// A read-only static property:
public static int Counter
{
get
{
return counter;
}
}

// Constructor:
public Employee()
{
// Calculate the employee's number:
counter = ++counter + numberOfEmployees;
}
}

////////////////////////////////////////////////////////////

public class MainClass
{
public static void Main()
{
Employee.numberOfEmployees = 100;
Employee e1 = new Employee();
e1.Name = "Claude Vige";
Console.WriteLine("Employee number: {0}", Employee.Counter);
Console.WriteLine("Employee name: {0}", e1.Name);
}
}






Register or Login to Post Your Opinion