Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a type to a method?

Tags:

c#

How can I call this constructor ?

public class DataField
{
    public String Name;
    public Type TheType;
    public DataField(string name, Type T)
    {
        Name = name;
        TheType = T;
    }
}

I thought of

f = new DataField("Name",typeof(new String()));

but I want to avoid object creation. So is this one ok ?

f = new DataField("Name",String);
like image 845
bokan Avatar asked Nov 26 '12 15:11

bokan


People also ask

How do you pass values to the method?

Both values and references are stored in the stack memory. Arguments in Java are always passed-by-value. During method invocation, a copy of each argument, whether its a value or reference, is created in stack memory which is then passed to the method.

Can an object be passed to a method?

The basic data types can be passed as arguments to the C# methods in the same way the object can also be passed as an argument to a method. But keep in mind that we cannot pass a class object directly in the method. We can only pass the reference to the object in the method.

How do you pass a string to a method in Java?

Method signaturepublic static void myMethod(String fname) ; The method takes the String parameter and then appends additional text to the string and then outputs the value to the console. The method is invoked from the method by passing some sample strings with male names.


2 Answers

You can use a type name with typeof:

f = new DataField("Name", typeof(string));
like image 165
Mark Byers Avatar answered Oct 03 '22 07:10

Mark Byers


You should be able to use simply typeof(string)

like image 22
Fiona - myaccessible.website Avatar answered Oct 03 '22 06:10

Fiona - myaccessible.website