Last active
March 14, 2019 22:00
-
-
Save PadreSVK/0d6c4cd464e0a41135eadc508510285f to your computer and use it in GitHub Desktop.
Sample snippets of assign initial value for c# property = ICS course
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ICS_project.DAL.Entities | |
{ | |
public abstract class MessageBase : EntityBase | |
{ | |
// compiler generate for you code from MessageBase1.cs, and initialize property with default value | |
public DateTime CreationDate { get; } = DateTime.Now; | |
public string Content { get; set; } | |
public UserEntity User { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ICS_project.DAL.Entities | |
{ | |
public abstract class MessageBase : EntityBase | |
{ | |
private DateTime dateTime = DateTime.Now | |
// property can be set only from this scope (from method or ctor) | |
public DateTime CreationDate | |
{ | |
get => dateTime; | |
private set => dateTime = value; | |
} | |
public string Content { get; set; } | |
public UserEntity User { get; set; } | |
} | |
} | |
// this proeprty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ICS_project.DAL.Entities | |
{ | |
public abstract class MessageBase : EntityBase | |
{ | |
// same behavior as previous sample = MessageBase1.cs | |
protected MessageBase(){ | |
CreationDate = DateTime.Now; | |
} | |
public DateTime CreationDate { get; } | |
public string Content { get; set; } | |
public UserEntity User { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment