Is it possible to add Extra custom keyword to c# program so that makes two int variables different types? -
i need create 2 keywords input , output can have these decelerations in c# code:
input int a; input int b; output int c; swap (input int lhs, output int rhs) { ... } swap(a,c) // should compiled; swap(c,a) // should return compile error swap(a,b) // should return compile error i need compiler accept a = b reject b = c or c = a. possible? if not possible solution? use output<t> , input<t> generic wrappers types hate using value getter , setter whenever want access values inside these generic wrappers!
you can use this:
public sealed class input<t> { public t value { get; set; } public input(t v) { value = v; } public static implicit operator t(input<t> d) { return d.value; } public static implicit operator input<t>(t d) { return new input<t>(d); } } public sealed class output<t> { public t value { get; set; } public output(t v) { value = v; } public static implicit operator t(output<t> d) { return d.value; } public static implicit operator output<t>(t d) { return new output<t>(d); } } then swap method looks like:
static void swap<t>(input<t> input, output<t> output) { output.value = input.value; } and usage:
input<int> myinput = 1; input<int> myinput2 = 1; output<int> myoutput = 0; swap(myinput, myoutput); //compiles swap(myinput, myinput2); //error swap(myoutput, myinput); //error
Comments
Post a Comment