Properties in C #
IT Tricks

Properties in C #


Properties – Property are named members of the class, structure, and Interface. Member variables or methods in a class or structure are called fields. Attributes are an inheritance of these Field and accessed by using the same syntax. They use accessor Through the values ​​of the Private Field can be read, written and manipulated.

Property does not name the storage locations. Instead, they have accessors that read, write or calculate their values.

For example, we have a class named Student, with Private Fields for age, name, and code. We cannot directly access these fields from outside the class scope, but we can have properties to access these private fields.

Accessor in C #

In C#, accessor is an attribute that contains executable commands, which help in retrieving (reading or calculating) or setting (writing) attributes. Accessor declarations can obtain a get accessor, an accessor set, or both. For example:

// khai báo một thuộc tính Code có kiểu dữ liệu string: public string Code { get { return code; } set { code = value; } } // khai báo một thuộc tính Name có kiểu dữ liệu String: public string Name { get { return name; } set { name = value; } } // khai báo một thuộc tính Age có kiểu dữ liệu int: public int Age { get { return age; } set { age = value; } }

For example

The following example illustrates the usage of attributes in C #: create 2 classes named respectively Student, TestCsharp as follows:

Class student:

using System; namespace QTMCsharp { class Student { private string code = "N/A"; private string name = "unknown"; private int age = 0; // khai bao thuoc tinh Code co kieu string: public string Code { get { return code; } set { code = value; } } // khai bao thuoc tinh Name co kieu string: public string Name { get { return name; } set { name = value; } }   // khai bao thuoc tinh Age co kieu int: public int Age { get { return age; } set { age = value; } } public override string ToString() { return "MSSV = " + Code + ", Ho Ten = " + Name + ", Tuoi = " + Age; } } }

Class TestCsharp:

using System;
using System.Reflection;

namespace QTMCsharp
{
class TestCsharp
{
static void Main(string[] args)
{
Console.WriteLine("Thuoc tinh (Property) trong C#");
Console.WriteLine("------------------------------------");

// tao mot doi tuong Student
Student s = new Student();

// thiet lap cac thuoc tinh code, name va age cho Student
s.Code = "001";
s.Name = "Minh Chinh";
s.Age = 21;
Console.WriteLine("Thong tin sinh vien: {0}", s);

//bay gio tang age them 1
s.Age += 1;
Console.WriteLine("Thong tin sinh vien: {0}", s);

Console.ReadLine();
Console.ReadKey();
}
}
}

If you do not use the command Console.ReadKey(); the program will run and end (so fast that you can not see the results in time). This command allows us to see the result more clearly.

Compiling and running the above C # program will produce the following result:

Accessor in C #

Abstract properties in C #

An Abstract class may have an abstract property, which should be implemented in the derived class. The following program illustrates this:

Create 3 layers named respectively Person, Student, TestCsharp as follows:

Class abstract Person:

sing System; namespace QTMCsharp { public abstract class Person { public abstract string Name { get; set; } public abstract int Age { get; set; } } }

Class student:

using System;

namespace QTMCsharp
{
class Student : Person
{
private string code = "N/A";
private string name = "N/A";
private int age = 0;

// khai bao thuoc tinh Code co kieu string:
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}

// khai bao thuoc tinh Name co kieu string:
public override string Name
{
get
{
return name;
}
set
{
name = value;
}
}

// khai bao thuoc tinh Age co kieu int:
public override int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public override string ToString()
{
return "MSSV = " + Code + ", Ho ten = " + Name + ", Tuoi = " + Age;
}
}
}

Class TestCsharp

using System;
using System.Reflection;

namespace QTMCsharp
{
class TestCsharp
{
static void Main(string[] args)
{
Console.WriteLine("Thuoc tinh (Property) trong C#");
Console.WriteLine("------------------------------------");

// tao mot doi tuong Student
Student s = new Student();

// thiet lap code, name va age cho Student
s.Code = "001";
s.Name = "Minh Chinh";
s.Age = 21;
Console.WriteLine("Thong tin sinh vien: {0}", s);

//bay gio tang age them 1
s.Age += 1;
Console.WriteLine("Thong tin sinh vien: {0}", s);

Console.ReadLine();
Console.ReadKey();
}
}
}

Compiling and running the above C # program will produce the following result:

Abstract properties in C #

Follow Tutorialspoint

Previous lesson: Printing pages in JavaScript

Next: Objects in JavaScript

.

Leave a Reply

Your email address will not be published. Required fields are marked *