Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether a type is string in C#?

Tags:

c#

I want to go through all the properties of a type and want to check whether a property type is not a string, how can I do it ?

My class is:

public class MarkerInfo {     public string Name { get; set; }     public byte[] Color { get; set; }     public TypeId Type { get; set; }     public bool IsGUIVisible { get; set; }      public MarkerInfo()     {         Color = new byte[4]; // A, R, G, B         IsGUIVisible = true;     } } 

the code I am using to check for type is:

foreach (var property in typeof(MarkerInfo).GetProperties()) {                    if (property.PropertyType is typeof(string))               } 

But this code is not working, any idea how to do that ?

like image 322
Embedd_0913 Avatar asked Mar 01 '12 13:03

Embedd_0913


People also ask

How do you check if it is a string in C?

You can call isdigit() on each character of the string, and if it's true for all characters you have an integer, otherwise it's some alphanumeric string. You can also call strtol to parse the string as an integer. The second argument returns a pointer to the first non-numeric character in the string.

How do you check if an element is a string?

Use the typeof operator to check if a variable is a string, e.g. if (typeof variable === 'string') . If the typeof operator returns "string" , then the variable is a string. In all other cases the variable isn't a string. Copied!

What data type is a string in C?

The C language does not have a specific "String" data type, the way some other languages such as C++ and Java do. Instead C stores strings of characters as arrays of chars, terminated by a null byte.

Is %s string in C?

The C language does not provide an inbuilt data type for strings but it has an access specifier “%s” which can be used to directly print and read strings. You can see in the above program that string can also be read using a single scanf statement.


1 Answers

if (property.PropertyType == typeof(string)) 
like image 166
Darin Dimitrov Avatar answered Oct 01 '22 01:10

Darin Dimitrov