I have problem with understanding generic method in interface. I'm using .NET framework 4.8 with auto C# version so it should be C# 7.3. Example below
Interface is:
public interface IRegister
{
Nullable<T> Read<T>() where T: struct;
void Write<T>(T value) where T : struct;
}
Now some class inherit from interface:
public abstract class AbstractRegister : IRegister
{
protected ModbusClient client;
protected int Address;
public abstract Nullable<T> Read<T>() where T : struct;
public virtual void Write<T>(T value) where T : struct
{
return;
}
public AbstractRegister(int address, ModbusClient client)
{
this.client = client;
this.Address = address;
}
}
Now I have some class inherited from AbstractRegister:
class InputRegister_int : AbstractRegister
{
public InputRegister_int(int address, ModbusClient client) : base(address, client) { }
public override int? Read<int>()
{
if (client == null)
return null;
return this.client.ReadInputRegisters(this.Address, 1)[0] as int?;
}
}
and in line public override int? Read<int>() I have errors:
CS1001: Identifier expected
CS1003: Syntax error, '>' expected
CS1003: Syntax error, '(' expected
I'm using Visual Studio Community 2022
When I use Int32 instead int, everything works ok, and it almost resolve my problem, but type float (which is necessary for me) doesn't have Float representation.
I have no ideas for solving this problem anymore. Can someone explain it to me?