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:

csharp Copy
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

Explain Anonymous type in C#

middle

What is the difference between Equality Operator ( == ) and Equals() Method in C#?

middle

Explain the difference between Task and Thread in .NET

Bình luận

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

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