Skip to content

Instantly share code, notes, and snippets.

@PadreSVK
Last active March 14, 2019 22:00
Show Gist options
  • Save PadreSVK/0d6c4cd464e0a41135eadc508510285f to your computer and use it in GitHub Desktop.
Save PadreSVK/0d6c4cd464e0a41135eadc508510285f to your computer and use it in GitHub Desktop.
Sample snippets of assign initial value for c# property = ICS course
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; }
}
}
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
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