Skip to main content

What is the difference between ref and out keywords?

Junior C#
Quick Answer Both pass by reference but with different rules. ref requires the variable to be initialized before passing รขโ‚ฌโ€ the method can read and modify it. out does not require pre-initialization, but the method must assign a value before returning. Use out for returning multiple values from a method.

Answer

ref: The variable must be initialized before passing.

out: Initialization is not required before passing, but must be assigned inside the method.

S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice