CSharp - Value Types

/*

Author: Jiangong SUN

*/


For the purpose of introducing the value types of CSharp language, and making it more clearly to developers, I'm writing this post. I'll list some samples and try to figure out some common misunderstandings about the concepts. And I will update the articles from time to time when I think it's necessary and I have some new understanding about them.

I have made some conclusion about the value types in my another post which could be helpful.

Here we go !


1) Boolean type:

Boolean type has only two possible values: true, false.

Someone may think Boolean and bool are different when you create a new boolean variable. 


Next, I'll compare the code generated, you will find the code are identical.


In fact, "bool" is an alias for "System.Boolean". Just like "int" for "System.Int32" and "string" for "System.String".

You can see all the alias here


2) Integral Types

There is a predefined implicit conversion from sbyte to short, int, long, float, double, or decimal.


To declare a decimal value, you need to use "m" as suffix. like: 

decimal xD = 100.13m;







A delegate in C# is a type that represents a reference to a method with a specific signature. It is similar to a function pointer in C and C++, but it is a type-safe way to pass methods as parameters to other methods or to store references to methods. A thread delegate in C# is a delegate that can be used to execute a method in a separate thread. A thread is a lightweight process that can run concurrently with other threads in a program. By using threads, you can perform multiple tasks simultaneously, which can improve the performance of your application. To create a thread delegate in C#, you first need to define a method that you want to execute in a separate thread. This method must have the same signature as the delegate, which is typically defined using the delegate keyword followed by the return type and parameter types of the method. Once you have defined the method, you can create an instance of the delegate by passing a reference to the method as a parameter. You can then use the delegate to create a new thread using the Thread class, which provides methods for starting and stopping threads. Here is an example of how to create and use a thread delegate in C#: ``` // Define a method that will be executed in a separate thread public void MyMethod() { // Do some work here } // Define a delegate that has the same signature as MyMethod public delegate void MyDelegate(); // Create an instance of the delegate by passing a reference to MyMethod MyDelegate myDelegate = new MyDelegate(MyMethod); // Create a new thread using the delegate and start it Thread thread = new Thread(new ThreadStart(myDelegate)); thread.Start(); ``` In this example, the MyMethod method is defined with no parameters and no return value, which matches the signature of the MyDelegate delegate. An instance of the delegate is then created by passing a reference to MyMethod, and a new thread is created using the delegate and started using the Start method.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值