Share gettype c#

thaitam645

New member
#csharp #gettype #Programming #development ## gettype in c #

GetType là một từ khóa C# trả về loại biểu thức.Nó được sử dụng để có được loại thời gian chạy của một đối tượng, có thể khác với loại được khai báo của nó.Ví dụ: nếu bạn có một biến loại `int`, nhưng bạn gán một chuỗi cho nó,` gettype () `của biến đó sẽ trả về` System.String`.

## Cú pháp

Cú pháp của phương thức `gettype ()` như sau:

`` `C#
System.type gettype (obj đối tượng);
`` `

Trong đó `obj` là đối tượng mà bạn muốn nhận loại.

## ví dụ

Mã sau đây cho thấy cách sử dụng phương thức `gettype ()` để lấy loại đối tượng:

`` `C#
int i = 10;
Console.WriteLine (getType (i));// in "System.int32"

Chuỗi s = "Hello World";
Console.WriteLine (gettype (s));// in "System.String"
`` `

## Nhận xét

Phương thức `gettype ()` có thể được sử dụng để có được loại của bất kỳ đối tượng nào, bao gồm các đối tượng không thuộc loại đã biết.Ví dụ: mã sau đây cho thấy cách lấy loại đối tượng được tạo bằng cách sử dụng từ khóa `động ':

`` `C#
động d = đối tượng mới ();
Console.WriteLine (getType (d));// in "System.Object"
`` `

## Xem thêm

* [System.type] (Type Class (System))
* [Đối tượng] (Object Class (System))
* [Dynamic] (System.Dynamic Namespace)
=======================================
#csharp #gettype #Programming #development ## Gettype in C#

Gettype is a C# keyword that returns the type of an expression. It is used to get the runtime type of an object, which can be different from its declared type. For example, if you have a variable of type `int`, but you assign a string to it, the `Gettype()` of that variable will return `System.String`.

## Syntax

The syntax of the `Gettype()` method is as follows:

```c#
System.Type Gettype(object obj);
```

where `obj` is the object whose type you want to get.

## Examples

The following code shows how to use the `Gettype()` method to get the type of an object:

```c#
int i = 10;
Console.WriteLine(Gettype(i)); // Prints "System.Int32"

string s = "Hello world";
Console.WriteLine(Gettype(s)); // Prints "System.String"
```

## Remarks

The `Gettype()` method can be used to get the type of any object, including objects that are not of a known type. For example, the following code shows how to get the type of an object that is created using the `dynamic` keyword:

```c#
dynamic d = new object();
Console.WriteLine(Gettype(d)); // Prints "System.Object"
```

## See also

* [System.Type](https://docs.microsoft.com/en-us/dotnet/api/system.type)
* [Object](https://docs.microsoft.com/en-us/dotnet/api/system.object)
* [Dynamic](https://docs.microsoft.com/en-us/dotnet/api/system.dynamic)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top