Thursday, March 26, 2015
There are times when you see a class property without it's private member counterpart and all you see is the {set; get;} accessors. What you are looking at are auto properties that you can create in C#, the only requirements is that the set; and get; accessors contains no logic. Usually there are private members that the properties expose to other classes using the get/set accessors, like the code below:
To create immutable properties, all you have to do is add the keyword private to the set; accessors. Immutable properties, means that the properties are only changeable within the class itself. Outside classes can only read the properties.
public class Product { private int productId; private string name; private string description; private decimal price; public int ProductId { get { return productId; } set { productId = value; } } public string Name { get { return name; } set { name = value; } } public string Description { get { return description; } set { description = value; } } public decimal Price { get { return price; } set { price = value; } } }The above class uses the regular ways you define properties that are exposed to other classes. Even though there are only get/set properties. The auto implemented version of the class requires a lot less typing.
public class Product { public int ProductId { get; set; } public string Name { get; set;} public string Description { get; set; } public decimal Price { get; set; } }The properties do not have be define as a private member of the class. This pattern is called the field-backed property pattern. This means that the C# compiler will take care of the private members fields for us when we build our classes. The above example are mutable properties, meaning outside classes can change the values of the properties.
To create immutable properties, all you have to do is add the keyword private to the set; accessors. Immutable properties, means that the properties are only changeable within the class itself. Outside classes can only read the properties.
public class Product { public int ProductId { get; private set; } public string Name { get; private set;} public string Description { private get; set; } public decimal Price { get; private set; } }
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Aivivu - đại lý chuyên vé máy bay trong nước và quốc tế
ReplyDeletemua ve may bay di my
khi nào có chuyến bay từ mỹ về việt nam
lich bay tu duc ve viet nam
vé máy bay từ nga về việt nam
bay từ anh về việt nam
bay từ pháp về việt nam mấy tiếng
Chi phí cho chuyên gia nước ngoài