gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
c#. how to check that type is struct?
- User
c#. how to check that type is struct?
- Assistant
To check if a type is a struct (value type) in C#, you can use the
IsValueTypeproperty of theTypeclass. Here's an example:csharpusing System; class Program { static void Main() { // Example types Type intType = typeof(int); Type stringType = typeof(string); // Check if they are structs (value types) bool isIntStruct = intType.IsValueType; bool isStringStruct = stringType.IsValueType; Console.WriteLine("int is a struct (value type): " + isIntStruct); // True Console.WriteLine("string is a struct (value type): " + isStringStruct); // False } }In this example, we check if the
intandstringtypes are value types (structs). As expected,intis a struct, andstringis not.