browse by category or date

This is a continuation of my previous post, where I am trying to complete reading this book:

So, C# 7.0 also introduce pattern matching in switch statement. Consider below example function:

public static string detectParam(object o) {
	switch (o) {
		case int i when i > 10:
			return "Big Integer";
		case int i:
			return "Integer";
		case double d when d > 10:
			return "Big Double";
		case double d:
			return "Double";
		case string s:
			return "String";
		default:
			return "Object";
		case null:
			return "Null";
	}
}

We can then test the output of the function:

public static void Main()
{
	object o = 15;
	Console.WriteLine($"{o} is {detectParam(o)}"); // 15 is Big Integer
	
	object o1 = 9;
	Console.WriteLine($"{o1} is {detectParam(o1)}"); // 9 is Integer
	
	object o2 = 15.1;
	Console.WriteLine($"{o2} is {detectParam(o2)}"); // 15.1 is Big Double
	
	object o3 = 9.9;
	Console.WriteLine($"{o3} is {detectParam(o3)}"); // 9.9 is Double
	
	object o4 = "15";
	Console.WriteLine($"{o4} is {detectParam(o4)}"); // 15 is String
	
	object o5 = new Object();
	Console.WriteLine($"{o5} is {detectParam(o5)}"); // System.Object is Object
	
	object o6 = null;
	Console.WriteLine($"{o6} is {detectParam(o6)}"); // is Null
}

We can actually shorten the switch statement even further:

public static string detectParam(object o) {
    var result = o switch {
		int i when i > 10 => "Big Integer",
		int i => "Integer",
		double d when d > 10 => "Big Double",
		double d => "Double",
		string s => "String",		
		null => "Null",
		_ => "Object"
	};
	return result;
}

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

It seems many people (including one famous celebrity) has been secretly violating their 14-days LoA (leave of absence). Thus, MOH is introducing new measure:

[Sent by Gov.sg – 19 Feb]

“Stay-Home Notice” (SHN) to fight COVID19

Who will be issued a SHN?
– S’pore Citizens, PRs, long-term pass holders and foreign employees issued with work passes, who have been in mainland China (outside of Hubei) in the last 14 days

What must you do during the SHN period
🏠 remain home at all times
🙅‍♀ minimise contact with others; no visitors
🧾 keep a record of persons you come into close contact with
🌡 monitor your health
🧼 maintain personal hygiene

Info on SHN: Go.gov.sg/StayHomeNotice

Leave of Absence Support Programme
– Extended to businesses and self-employed persons affected by SHNs
– Apply for $100 daily per worker on SHN

More: Go.gov.sg/loasp

Update on number of confirmed cases and recoveries:

Sent by Gov.sg]

COVID-19: 19 Feb Update

As of 12pm:
New cases: 3
Total confirmed: 84
Discharged today: 5
Total discharged: 34
Total still in hospital: 50

Most in hospital are stable or improving. 4 remain in ICU.

2 of the new cases have links to previous cases.

More: Go.gov.sg/moh19feb

Supporting Singaporeans impacted by COVID-19

Stabilisation and Support Package
– Help employers to retain workers & increase wages
– Corporate Income Tax rebates
– Flexible rental payments for Govt-managed properties
Go.gov.sg/support-companies-workers

Tourism, transport, retail and F&B sectors get additional support

Stay safe and vigilant everyone!

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

This is a part of series on my quest to devour this book and share any interesting things that I didn’t know before:

This book is currently only available at Amazon. Singapore’s National Library only have its previous edition:

So C# 7.0 introduces new syntax sugar in if-else statement. Say we receive an object as a parameter for arithmetic operation. To prevent runtime error, we need to ensure that the parameter is an integer. The old way of doing this would be:

public static int someArithmeticOp(object o) {
    try {
        var i = (int) o;		
        // ..
        // .. do the arithmetic ops here 
        //..
    }
    catch (Exception ex) {
        throw;
    }		
}

With C# 7.0, we can do this:

public static int someArithmeticOp(object o) {    
    if (o is int i) {		
        // ..
        // .. do the arithmetic ops here 
        //..
    }
    else {
        // o is not integer, you can throw Exception, or doing other useful steps
        throw new Exception("Invalid parameter");
    }		
}

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant: