Skip to content

Instantly share code, notes, and snippets.

using System;
namespace CSharp_Attributes_Walkthrough.My_Custom_Attributes
{
/// <summary>
/// This class is a custom attribute class.
/// Moreover, it is using the AttributeUsage attribute to annotate
/// that this attribute is applicable only to class, struct and property.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property)]
using System;
namespace CSharp_Attributes_Walkthrough.My_Custom_Attributes
{
public class AliasAttribute : Attribute
{
//This is how to define a custom attributes.
}
}
using System;
namespace CSharp_Attributes_Walkthrough.My_Custom_Attributes
{
public class AliasAttribute : Attribute
{
/// <summary>
/// These parameters will become mandatory once have you decided to use this attribute.
/// </summary>
/// <param name="alias"></param>
using CSharp_Attributes_Walkthrough.My_Custom_Attributes;
using System;
namespace CSharp_Attributes_Walkthrough.My_Custom_Attributes
{
public class AliasAttribute : Attribute
{
//....
//Added an optional-parameter
[Fact]
public void Test_GetAll_AttributeTargets()
{
var targets = Enum.GetNames(typeof(AttributeTargets));
foreach (var target in targets)
{
this._output.WriteLine($"AttributeTargets.{target}");
}
using CSharp_Attributes_Walkthrough.My_Custom_Attributes;
using System;
namespace Implementing_Csharp_Attributes_101
{
class Program
{
static void Main(string[] args)
{
var customer = new Customer { Firstname = "Jin Vincent" , LastName = "Necesario" };
using System;
using System.Linq;
namespace CSharp_Attributes_Walkthrough.My_Custom_Attributes
{
[Alias("Filipino_Customers", ConsoleColor.Yellow)]
public class Customer
{
[Alias("Fname", ConsoleColor.White, AlternativeName = "Customer_FirstName")]
public string Firstname { get; set; }
/*forEach loop*/
let amounts = [1.25, 2.25, 3.25, 4.25];
/**
* Outputs:
* 1.25
* 2.25
* 3.25
* 4.25
*/
const TOTAL_WORKING_DAYS = 261;
const getDailyRate = salary => Math.floor(((salary * 12) / (TOTAL_WORKING_DAYS)));
const dailyRate = employee => Object.assign({}, { employee: employee.employee, dailyRate: getDailyRate(employee.salary) });
//Returns a new set of empoloyees with dailyRate and name
const newEmployees = employees.map(dailyRate);
//new data
const TOTAL_WORKING_DAYS = 261;
//Horrible in my opinion, worst someone will say it is ugly.
const dailyRate = (item, index, array) => array[index].dailyRate = Math.floor(((item.salary * 12) / (TOTAL_WORKING_DAYS)));
//undefined as forEach doesn't return any results.
let dailyRateEmployeeResults = employees.forEach(dailyRate);
console.log(dailyRateEmployeeResults);//undefined