if (!(sender as RadioButton).Checked) TextBox tb = Controls["textBox" + i] as TextBox;
What is sender as and as?
if (!(sender as RadioButton).Checked) TextBox tb = Controls["textBox" + i] as TextBox;
What is sender as and as?
The as operator is used to perform certain types of conversions between compatible reference types and returns null if the conversion is not possible. sender in your case, most likely the source of the event.
Simply put, the as
operator is an operator to explicitly cast compatible reference types.
MSDN has a good example of how it works:
class csrefKeywordsOperators { class Base { public override string ToString() { return "Base"; } } class Derived : Base { } class Program { static void Main() { Derived d = new Derived(); Base b = d as Base; if (b != null) { Console.WriteLine(b.ToString()); } } } }
In this example, the class heir is cast to the type of its parent.
sender
- relates to events about which you can read on MSDN .
But, again, briefly: Almost every event has a signature recommended by everyone that looks like
void foo(EventArgs, object)
Where EventArgs is the function parameters, and object is the object which caused this event. Accordingly, EventArgs /*параметры функции*/
are called as arg
, and object /*вызвавший объект*/
as sender
. because object is an untyped object that needs a type conversion to work with.
That is, I dare to suggest, and happens in your question.
The object that triggered the event is cast to a specific type for working with it.
I hope I answered your question. Good luck!
Source: https://ru.stackoverflow.com/questions/94180/
All Articles