Câu hỏi phỏng vấn C#
Câu hỏi

What is the difference between ref and out keywords?

Câu trả lời

Here is the difference between the ref and out keywords in C#:

ref keyword

  • The ref keyword is used to pass an argument to a method by reference.
  • The variable passed to the method using ref must be initialized before passing it to the method.
  • The method can read and modify the value of the variable passed using ref.
  • The changes made to the variable inside the method are reflected in the original variable outside the method.

Example:

int x = 5;
DoubleValue(ref x);
Console.WriteLine(x); // Output: 10

out keyword

  • The out keyword is also used to pass an argument to a method by reference.
  • The variable passed to the method using out does not need to be initialized before passing it to the method.
  • The method must assign a value to the variable passed using out before returning.
  • The changes made to the variable inside the method are reflected in the original variable outside the method.

Example:...

middle

middle

Gợi ý câu hỏi phỏng vấn

middle

What is Virtual Method in C#?

middle

Why can't you specify the accessibility modifier for methods inside the Interface?

expert

Could you explain the difference between Func vs. Action vs. Predicate ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào