C# 我如何使用接口作为;“出去”;参数

C# 我如何使用接口作为;“出去”;参数,c#,out,C#,Out,当我尝试使用“out”关键字调用函数passing和dog类实例时,我收到一个错误: “方法的最佳重载…具有一些无效参数” 我怎样才能将实现接口的类传递给我的数据库函数以填充数据 更新: public interface IAnimal { } public interface IDog : IAnimal { } public class Dog : IDog { public bool has_two_legs = false; } public static class te

当我尝试使用“out”关键字调用函数passing和dog类实例时,我收到一个错误:

“方法的最佳重载…具有一些无效参数”

我怎样才能将实现接口的类传递给我的数据库函数以填充数据

更新:

public interface IAnimal
{
}

public interface IDog : IAnimal
{
}

public class Dog : IDog
{
    public bool has_two_legs = false;
}

public static class test
{
    public static void QueryAnimalProperties(out IAnimal animal_details)
    {
        //some sql queries
        animal_details.has_two_legs = true;
    }

    public static void Test()
    {
        Dog my_dog;
        test.QueryAnimalProperties(out my_dog);
    }
}
尝试键入强制转换输入也会出现错误:

ref或out参数必须是可赋值变量


这里没有理由使用
out
参数

从方法中删除它,只需更新传递给它的类的属性(可在
IAnimal
接口中找到),如果您打算这样做的话

test.QueryAnimalProperties(out (IAnimal)my_dog);

因为类已经是引用类型,所以当执行返回到名为
QueryAnimalProperties
的任何方法时,
Dog
的实例将保留您在其中设置的值。

这里没有理由使用
out
参数

从方法中删除它,只需更新传递给它的类的属性(可在
IAnimal
接口中找到),如果您打算这样做的话

test.QueryAnimalProperties(out (IAnimal)my_dog);

因为类已经是引用类型,所以当执行返回到名为
QueryAnimalProperties
的任何方法时,
Dog
的实例将保留您在其中设置的值。

如果不将对象实例作为
out
参数的输入值传递,该值将被丢弃

下面是一个简单的反例,演示了它为什么不起作用:

public void QueryAnimalProperties(IAnimal animal_details)
{
   //some sql queries

   animal_details.SomeAvailableProperty = "SomeValue";
}
//

在这种情况下,
QueryAnimalProperty
的实现是有效的,因为
仓鼠:IAnimal
animal\u details
可以在实现接口时接受
仓鼠
类型的对象

但是
queryanimalproperty
的调用方不能期望
animal\u details
最终解析为
Dog
,正如我的示例所示,它可能会将其设置为
仓鼠

作为一个旁白,我不认为您正确使用<代码> OUT/COD>:<代码> OUT/CODE>意味着引用本身被改变,好像它是C和C++中的值指针(或者这是堆对象,它是指针值的指针)。 将

out
params视为等同于函数的返回类型,从以下内容更改函数:

Dog dog;
QueryAnimalProperties(out dog);
为此:

public void QueryAnimalProperties(out IAnimal animal_details)

它的工作原理也是一样。

如果不将对象的实例作为
out
参数的输入值传递,该值将被丢弃

下面是一个简单的反例,演示了它为什么不起作用:

public void QueryAnimalProperties(IAnimal animal_details)
{
   //some sql queries

   animal_details.SomeAvailableProperty = "SomeValue";
}
//

在这种情况下,
QueryAnimalProperty
的实现是有效的,因为
仓鼠:IAnimal
animal\u details
可以在实现接口时接受
仓鼠
类型的对象

但是
queryanimalproperty
的调用方不能期望
animal\u details
最终解析为
Dog
,正如我的示例所示,它可能会将其设置为
仓鼠

作为一个旁白,我不认为您正确使用<代码> OUT/COD>:<代码> OUT/CODE>意味着引用本身被改变,好像它是C和C++中的值指针(或者这是堆对象,它是指针值的指针)。 将

out
params视为等同于函数的返回类型,从以下内容更改函数:

Dog dog;
QueryAnimalProperties(out dog);
为此:

public void QueryAnimalProperties(out IAnimal animal_details)

它的工作原理也是一样的。

查询IMALProperty
可以返回一个对象,该对象是
IAIMAL
,但不是
IDog
(例如
ICat
)。这样的对象将不能分配给
IDog
变量。因此,这是禁止的。

查询IMALProperty
可以返回一个对象,该对象是
IAIMAL
,但不是
IDog
(例如
ICat
)。这样的对象将不能分配给
IDog
变量。因此,这是禁止的。

您不需要out参数

但是如果您想使用它,那么就使用带有约束的通用方法

public IAnimal QueryAnimalProperties()

Dog dog = QueryAnimalProperties(); // invalid, QueryAnimalProperties is not guaranteed to return Dog.
公共接口IAnimal
{
字符串名称{get;set;}
}
公共接口IDog:IAnimal
{
}
公共空区QueryanimalProperty(外动物)
其中T:IAnimal,new()
{
动物=新T();
animal.Name=“Fred”;
}    
公家犬:IDog
{
公共字符串名称{get;set;}
}
void Main()
{
狗;
QueryAnimalProperties(out dog);
Console.WriteLine(dog.Name);
}
请注意,如果在不修改其余代码的情况下删除out参数,那么应用程序基本上取决于所谓的副作用,在这种情况下,您希望避免这种副作用


您不需要out参数

但是如果您想使用它,那么就使用带有约束的通用方法

public IAnimal QueryAnimalProperties()

Dog dog = QueryAnimalProperties(); // invalid, QueryAnimalProperties is not guaranteed to return Dog.
公共接口IAnimal
{
字符串名称{get;set;}
}
公共接口IDog:IAnimal
{
}
公共空区QueryanimalProperty(外动物)
其中T:IAnimal,new()
{
动物=新T();
animal.Name=“Fred”;
}    
公家犬:IDog
{
公共字符串名称{get;set;}
}
void Main()
{
狗;
QueryAnimalProperties(out dog);
Console.WriteLine(dog.Name);
}
请注意,如果在不修改其余代码的情况下删除out参数,那么应用程序基本上取决于所谓的副作用,在这种情况下,您希望避免这种副作用


这失败了,因为
Dog
是一个具体的类

public interface IAnimal
{
    string Name { get; set; }
}

public interface IDog : IAnimal
{

}

public void QueryAnimalProperties<T>(out T animal)
where T : IAnimal, new()
{
    animal = new T();
    animal.Name = "Fred";    
}    

public class Dog : IDog
{
    public string Name { get; set; }
}

void Main()
{
    Dog dog;
    QueryAnimalProperties(out dog);
    Console.WriteLine(dog.Name);
}
只承诺一只
IAnimal
不一定是一只
Dog

要修复此问题,请将类型从
Dog
编辑为
IAnimal

public static void QueryAnimalProperties(out IAnimal animal_details)
公共静态无效测试()
{

IAnimal my_dog;//这失败了,因为
dog
是一个concre