简体   繁体   English

如何获取C#中变量的数据类型?

[英]How can I get the data type of a variable in C#?

How can I find out what data type some variable is holding?我怎样才能找出某个变量保存的数据类型? (eg int, string, char, etc.) (例如 int、string、char 等)

I have something like this now:我现在有这样的东西:

private static void Main()
{
   var someone = new Person();
   Console.WriteLine(someone.Name.typeOf());
}

public class Person
{
    public int Name { get; set; }
}

How can I find out what data type some variable is holding?我如何找出某个变量持有的数据类型? (eg int, string, char, etc.) (例如int,string,char等)

I have something like this now:我现在有这样的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Testing
{
    class Program
    {
        static void Main()
        {
            Person someone = new Person();
            someone.setName(22);
            int n = someone.getName();
            Console.WriteLine(n.typeOf());
        }
    }

    class Person
    {
        public int name;

        public void setName(int name)
        {
            this.name = name;
        }

        public int getName()
        {
            return this.name;
        }
    }
}

How can I find out what data type some variable is holding?我如何找出某个变量持有的数据类型? (eg int, string, char, etc.) (例如int,string,char等)

I have something like this now:我现在有这样的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Testing
{
    class Program
    {
        static void Main()
        {
            Person someone = new Person();
            someone.setName(22);
            int n = someone.getName();
            Console.WriteLine(n.typeOf());
        }
    }

    class Person
    {
        public int name;

        public void setName(int name)
        {
            this.name = name;
        }

        public int getName()
        {
            return this.name;
        }
    }
}

How can I find out what data type some variable is holding?我如何找出某个变量持有的数据类型? (eg int, string, char, etc.) (例如int,string,char等)

I have something like this now:我现在有这样的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Testing
{
    class Program
    {
        static void Main()
        {
            Person someone = new Person();
            someone.setName(22);
            int n = someone.getName();
            Console.WriteLine(n.typeOf());
        }
    }

    class Person
    {
        public int name;

        public void setName(int name)
        {
            this.name = name;
        }

        public int getName()
        {
            return this.name;
        }
    }
}

How can I find out what data type some variable is holding?我如何找出某个变量持有的数据类型? (eg int, string, char, etc.) (例如int,string,char等)

I have something like this now:我现在有这样的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Testing
{
    class Program
    {
        static void Main()
        {
            Person someone = new Person();
            someone.setName(22);
            int n = someone.getName();
            Console.WriteLine(n.typeOf());
        }
    }

    class Person
    {
        public int name;

        public void setName(int name)
        {
            this.name = name;
        }

        public int getName()
        {
            return this.name;
        }
    }
}

How can I find out what data type some variable is holding?我如何找出某个变量持有的数据类型? (eg int, string, char, etc.) (例如int,string,char等)

I have something like this now:我现在有这样的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Testing
{
    class Program
    {
        static void Main()
        {
            Person someone = new Person();
            someone.setName(22);
            int n = someone.getName();
            Console.WriteLine(n.typeOf());
        }
    }

    class Person
    {
        public int name;

        public void setName(int name)
        {
            this.name = name;
        }

        public int getName()
        {
            return this.name;
        }
    }
}

How can I find out what data type some variable is holding?我如何找出某个变量持有的数据类型? (eg int, string, char, etc.) (例如int,string,char等)

I have something like this now:我现在有这样的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Testing
{
    class Program
    {
        static void Main()
        {
            Person someone = new Person();
            someone.setName(22);
            int n = someone.getName();
            Console.WriteLine(n.typeOf());
        }
    }

    class Person
    {
        public int name;

        public void setName(int name)
        {
            this.name = name;
        }

        public int getName()
        {
            return this.name;
        }
    }
}

How can I find out what data type some variable is holding?我如何找出某个变量持有的数据类型? (eg int, string, char, etc.) (例如int,string,char等)

I have something like this now:我现在有这样的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Testing
{
    class Program
    {
        static void Main()
        {
            Person someone = new Person();
            someone.setName(22);
            int n = someone.getName();
            Console.WriteLine(n.typeOf());
        }
    }

    class Person
    {
        public int name;

        public void setName(int name)
        {
            this.name = name;
        }

        public int getName()
        {
            return this.name;
        }
    }
}

PHP and C# are related syntactically but quite different, whilst I could answer the question at face value (See this article http://msdn.microsoft.com/en-us/library/58918ffs(v=vs.71).aspx ) I strongly advise you to get hold of a copy of CLR via C# (Third or second edition) by Jeffrey Richter and read it. PHP和C#在语法上相关,但是有很大的不同,尽管我可以从表面上回答这个问题(请参阅本文http://msdn.microsoft.com/zh-cn/library/58918ffs(v=vs.71).aspx )我强烈建议您通过Jeffrey Richter的C#(第三版或第二版)获得CLR副本并阅读。 Its the finest book related to programming I think I've ever read and would answer almost all of your type related questions and give you a very deep understanding of what's going on under the hood! 它是与编程有关的最好的书,我想我已经读过,并且可以回答几乎所有与类型有关的问题,并且可以使您对幕后的情况有深刻的理解!

How can I find out what data type some variable is holding?我如何找出某个变量持有的数据类型? (eg int, string, char, etc.) (例如int,string,char等)

I have something like this now:我现在有这样的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Testing
{
    class Program
    {
        static void Main()
        {
            Person someone = new Person();
            someone.setName(22);
            int n = someone.getName();
            Console.WriteLine(n.typeOf());
        }
    }

    class Person
    {
        public int name;

        public void setName(int name)
        {
            this.name = name;
        }

        public int getName()
        {
            return this.name;
        }
    }
}

Use the Object.GetType Method, this will do the job.使用Object.GetType方法,这将完成这项工作。

If you just wanna know the type of a variable:如果您只想知道变量的类型:

var test = (byte)1;
Console.WriteLine(test.GetType());
using System;

using System.ComponentModel.DataAnnotations;

class Program
{

    static void Main()
    {
        int x = Convert.ToInt16(Console.ReadLine());
        Console.WriteLine(x.GetType());//Returns type of x 
    }

}

//Code from visual Studio written in c#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM