DEV Community

Vatthanachai Wongprasert
Vatthanachai Wongprasert

Posted on • Edited on

2

C# Functional แบบงงๆ

สำหรับใครที่เป็นแฟนพันธุ์แท้ C# จะรู้ว่าความสามารถใหม่ (เมื่อนานมาแล้ว) จะสามารถเขียน Functional เหมือนกับภาษา F# หรือ Scala ได้ ซึ่งมันเป็นอะไรที่ว้าวมาก... (โดยเฉพาะผมที่พึ่งได้มีเวลาพักหายใจ)
เข้าเรื่องกันเลยดีกว่า

I know the Greek C# already know the new feature of her. Her have the functional feature like F# or Scala. But for me, this feature is Wowww.

Ok, let's start.

เริ่มแรกผมจะสร้าง Console App ขึ้นมาโดยจะเลือก Target Framework ไปที่ 4.8 ส่วนชื่อโปรเจคก็ตามใจชอบ หลังจากสร้างโปรเจคใหม่แล้ว ตัว Visual Studio ก็จะเปิดไฟล์ Program.cs ขึ้นมา ซึ่งรูปแบบคำสั่งเองก็จะมีลักษณะ ดังนี้

First I will create the console application and select the project target to .Net Framework 4.8, but for the project name; I think you can enter from your need. After create, We will get the code like this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloFunctional
{
class Program
{
static void Main(string[] args)
{
}
}
}
view raw Program.cs hosted with ❤ by GitHub

จากนั้นเราก็มา Re-Factor Code กันใหม่นิดหนึ่ง (ซึ่งเอาจริงๆ ไม่ต้องทำก็ได้ ข้ามไปเลยก็ได้ ฮ่าๆ)

Now, I have re-factor the code by removing unused code. and this is the result of re-factor. or you can skip this step.

using System;
namespace HelloFunctional
{
public class Program
{
public static void Main(string[] args)
{
}
}
}
view raw Program.cs hosted with ❤ by GitHub

หลังจากไร้สาระกันไปแล้ว ต่อไปเราก็จะเริ่มเขียนฟังกชั่นง่ายๆ ตัวนึงขึ้นมา โดยที่โค๊ดจะมีดังนี้

Next, I have writing some functional like this.

using System;
namespace HelloFunctional
{
public class Program
{
public static void Main(string[] args)
{
Func<int, int> f = (x) => x + 2;
int i = f(1);
Console.WriteLine(i);
Console.WriteLine("Press [Enter] to exist.");
Console.ReadLine();
}
}
}
view raw Program.cs hosted with ❤ by GitHub

และเมื่อเราทดลองรันโปรแกรมของเราดู ก็จะได้ผลลัพท์ เท่ากับ 3 ...
ว่าแต่.. Dev.to ทำใมใส่รูปได้แค่รูปเดียวเองแหะ... เอาเป็นว่าลองไปรันคำสั่งใน dotnetfiddle เอานะครับ

And when I executed. the result is 3.
I don't know why 'Dev.to' can insert only 1 image. Don't worry, please use the below link to test on the Dotnet Fiddle.

https://dotnetfiddle.net/Widget/NHqzJa

จากโค๊ด ... เราได้กำหนดให้ฟังก์ชัน f รับค่า x เข้ามาและนำ x ไปบวกด้วย 2 ซึ่งถ้าเราป้อนค่าให้กับ x โดนกำหนดให้เป็น 1 ผลลัพธ์ที่ได้ก็จะมีค่าเท่ากับ 3 นั่นเอง

From the Functional... I create the f function has the x parameter, and take him to add 2. and when I set x value is 1, the result is 3.

ทีนี้เราจะมาลองเขียนโค๊ดชุดนี้กันแบบสั้นๆ โดยจะเขียนคำสั่งดังนี้

Next, I will create a short functional like this.

using System;
namespace HelloFunctional
{
public class Program
{
public static void Main(string[] args)
{
Func<int, int> f = (x) => x + 2;
int i = f(1);
Console.WriteLine(i);
f = (x) => x + 2;
i = f(1);
Console.WriteLine(i);
Console.WriteLine("Press [Enter] to exist.");
Console.ReadLine();
}
}
}
view raw Program.cs hosted with ❤ by GitHub

และหลังจากทดลองรันดูแล้ว ผลลัพธ์ที่ได้ก็จะเท่ากับ 3 เหมือนกับข้างบน

After executed. The result is 3. Why the result like the previous function?

https://dotnetfiddle.net/Widget/B4yI3i

โอ้ววววว ไม่ต้องใส่ Func นำหน้า f ก็สามารถทำงานได้เหมือนกัน แจ่มปะละ ซึ่งก็จะรวมไปถึง i ที่เมื่อรับค่าจาก f แล้วก็ไม่จำเป็นต้องใส่ int เพราะค่าที่ได้ยังไงก็ได้กลับมาเป็น int แน่นอน

Because of the c# can use the shortcode.The 'f' gets the 'x' parameter and add '2' to 'x';
when I set 'i' to get the value from 'f', The 'i' will get the result from 'f'.

นี่ละที่เป็นความสามารถใหม่ที่ทำให้ว้าวจริงๆ เอาเป็นว่าพอแค่นี้ก่อนดีกว่าง่วงนอนมากกก ไว้ว่างๆ มีเวลาหายใจเดี๋ยวค่อยมาลองเขียน function ใหม่ๆ กันดู

I think this feature is very new for me haha.

ส่งท้ายขยายความ... Func คืออะไร...

Finally, What is the 'Function'?

Func => Function
และค่าที่อยู่ใน <> จะมีความหมายคือ..
โดยที่ input type จะสามารถรับได้ที่ 16 ตัว และ Result 1 ตัว

Func is from Function and the first type in '<>' is the input type and the final type is always is the result type.

Func < T1, T2, T3, ... T16, Result > (พิมพ์ติดแล้วกลายเป็น markdown / I cannot type without space, because this code will change to the markdown.)

ในตัวอย่าง Func f = (x) => x + 2; นั้น หากเราเอามาเขียนเป็น Full Function ก็จะได้คำสั่งแบบนี้

From this function 'Func f = (x) => x + 2;'. If we want to create the full function. This is the code. Haha.

using System;
namespace HelloFunctional
{
public class Program
{
public static void Main(string[] args)
{
//Function ย่อ
Func<int, int> f = (x) => x + 2;
int i = f(1);
Console.WriteLine(i);
//Function ย่อได้อีก
f = (x) => x + 2;
i = f(1);
Console.WriteLine(i);
//function แบบเต็มๆ
var result = FunctionA(1);
Console.WriteLine(result);
Console.WriteLine("Press [Enter] to exist.");
Console.ReadLine();
}
//function แบบเต็มๆ
public static int FunctionA(int x)
{
return x + 2;
}
}
}
view raw Program.cs hosted with ❤ by GitHub

Top comments (1)

Collapse
 
aixasz profile image
Nattapong Nunpan

จริงๆ Func มีมานานแล้วนะครับ และจริงๆ มันคือ delegate type ที่สามารถรับค่าในรูปแบบฟังก์ชั่นได้

ขอเสริมส่วนที่ ไม่ต้องใส่ Func นำหน้า f ก็สามารถทำงานได้เหมือนกัน

เพราะ f นั้นถูกประกาศเป็น type Func ข้างบนอยู่แล้ว

และ จากบรรทัด f = (x) => x + 2; มันคือการ mutate โดยการเอา ฟังก์ชั่น x + 2 มาเขียนทับใน f

ดังนั้นมันก็ไม่จำเป็นต้องประกาศใหม่

อารมณ์เหมือนประกาศ int x = 1; แล้วเรา mutate มันด้วย x = 1; อีกรอบนั้นเอง

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay