C#

C#: 22-Named and Optional Arguments

Optional Arguments

When arguments are given default values, they become optional

constants can be used


 

    class Logger
    {
        //using Named argument requires static constant variable...
        //..... const is static, so its redundant to use static
        //static string DefaultSystemName = "EmployeeTracker";
        const string DefaultSystemName = "EmployeeTracker";

        //priority type & system that throws log msg
        public static void Log(string msg, string system = DefaultSystemName, int priority=1)
        {
            Console.WriteLine("System: {0}, Priority: {1}, Msg: {2}", system , priority, system);
        }
    }


 

Named Arguments

Name arguments can be used in cases where a placeholder is needed for multi-argument methods with optional arguments

Tags

C#: 21-Extension Methods

Declaration

        // this extends the 'string' type
        // this is self-referencing
        // returns int
        public static int toInt(this string value)
        {
            return int.Parse(value);
        }

Usage

return Console.ReadLine().toInt();


 

Tags

C#: 20-Lambda Expressions

Compressed logic for predicate functions

static void ShowGrade(string name)
{
    //Predicate logic
    var found = employees.Find(predicate);
    Console.WriteLine("Predicate: {0}'s Grade: {1}", found.Name, found.Score);

    //lambda logic
    var foundLambda = employees.Find((employee) =>
    {
        return (employee.Name == "Jones");
    }
    );
    Console.WriteLine("Lambda: {0}'s Grade: {1}", found.Name, found.Score);
} 


 

Final 'uber' compressed version

            var found = employees.Find(employee => employee.Name == name);
            Console.WriteLine("lambda4: {0}'s Grade: {1}", found.Name, found.Score);
Tags

C#: 19-Predicate Functions

always returns a boolean

Main logic

ShowGrade("");


 

Method using predicate

static void ShowGrade(string name)
{
    //Predicate logic
    var found = employees.Find(predicate);
    Console.WriteLine("{0}'s Grade: {1}", found.Name, found.Score);
} 


 

Predicate Method

static bool predicate(Employee employee)
{
    //can return the condition instead of doing evail with if/then
    return (employee.Name == "Jones");

    //if (employee.Name == "Jones")
    //    return true;
    //else
    //    return false;
}
Tags

C#: 18-Anonymous Types

No class needed in advance

can create on the fly

good for data that changes structure or datatypes

NoSQL Dbs don't have datatypes for its fields, so no way to know ahead of time

var someObject = new {name = “Alex”, age=35}
Tags

C#: 17-Auto-Implemented Properties

Before

    class Member
    {
         public string Name;
        public string Address;
         protected double phone;
 
        public double Phone
        { 
            set { phone = value; }
        }
    }

After

class Member

{

public string Name { get; set; }

public string Address { get; set; }

public double Phone { get; set; }

}

Tags

C#: 16-Abstract Classes

Like a combo of Interfaces and Classes

Given the following Classes

    public class GeometryTool
    {
        public static void Run()
        {
            // fields
            var square = new Square() { Width = 2 };
            var triangle = new Triangle() { Base = 2, Height = 5 };

            Console.WriteLine(square.GetArea());
            Console.WriteLine(triangle.GetArea());
        }
    }


    class Square
    {
        public int Width;

        public int GetArea()
        {
            return Width * Width;
        }
    }
    class Triangle
    {
        public int Base;
        public int Height;

        public int GetArea()
        {
            return (Base * Height)/ 2;
        }
    }


 

Creating a shape abstract class, allows for merging similar function names and using override methods

Interfaces have no logic whereas Abstract Classes an implemented functions

Tags

C#: 15-Events

Declare the event at the class level

public static event Action Triggered;

Fire the Action

Triggered();

Add event listener method

        void HasTriggered()
        {
            Console.WriteLine("Survey Triggered, run stats");
        }


 

Register the event listener in a method

        public void Start()
        {
            Survey.Triggered += HasTriggered;
        }


 

When Triggered() is called


 

Definition Class with Inititlization and Caller

Add the event to class fields and give name

Instantiate the Implementation Class that will do something as a result of the event firing

Tags
Subscribe to C#