Methods అనేవి class లో నిర్వచించిన functions. C# గొప్ప parameter features లను supporting చేస్తుంది — optional/named arguments, ref/out/in modifiers, arrays — మరియు (ఒకే పేరుతో కానీ విభిన్న parameters తో అనేక methods).
paramspublic int Add(int a, int b) => a + b; // expression-bodied (concise)
public string Greet(string name, string greeting = "Hello") // default parameter
{
return $"{greeting}, {name}";
}
Greet("Ann"); // "Hello, Ann"
Greet("Ann", greeting: "Hi"); // named argument
Methods return type, name, మరియు parameters నిర్దేశించుకుంటాయి. Default parameters arguments ను optional చేస్తాయి, మరియు named arguments మీరు పేరు ద్వారా pass చేయడానికి అనుమతిస్తాయి (స్పష్టమైనది, optional లను skip చేయవచ్చు).
// ref — pass by reference (method can modify the caller's variable)
void Increment(ref int x) { x++; }
int n = 5; Increment(ref n); // n is now 6
// out — for returning multiple values (must be assigned in the method)
bool TryParse(string s, out int result) { result = ...; return true; }
if (int.TryParse("42", out int value)) { } // the common TryX pattern
// in — pass by reference but READ-ONLY (performance for large structs)
void Process(in LargeStruct data) { }
out TryParse pattern లో విస్తృతంగా ఉపయోగించబడుతుంది (success flag మరియు result ను సురక్షితంగా returning); ref caller's variable ని modify చేయడానికి అనుమతిస్తుంది; in large structs ను efficiently pass చేస్తుంది copying లేకుండా.
// params — variable number of arguments
int Sum(params int[] numbers) => numbers.Sum();
Sum(1, 2, 3, 4); // any number
// OVERLOADING — same name, different parameters
void Print(int x) { }
void Print(string s) { }
void Print(int x, int y) { }
Print(5); // calls the int version — resolved by argument types
Overloading బహుళ methods ను ఒక పేరు share చేయడానికి అనుమతిస్తుంది, parameter types/count ద్వారా వేరుగా చేయబడినవి — compiler matching one ని ఎంచుకుంటుంది. params variable number of arguments ను accept చేస్తుంది.
Methods C# లో behavior యొక్క building blocks, మరియు వాటి rich parameter features ను అర్థం చేసుకోవడం flexible, expressive code writing కోసం important.
Methods ను ఎలా నిర్వచించాలో (concise expression-bodied syntax సహా), default మరియు named parameters ఉపయోగించుకోవడం (optional arguments మరియు readable calls కోసం), మరియు method overloading apply చేయడం (ఒకే పేరు, విభిన్న parameters — intuitive APIs కోసం వివిధ argument types నిర్వహించేందుకు) తెలుసుకోవడం everyday knowledge.
Parameter modifiers ముఖ్యంగా అర్థం చేసుకోవడానికి ముఖ్యమైనవి: out ubiquitous TryParse pattern లో విస్తృతంగా ఉపయోగించబడుతుంది (int.TryParse(s, out var result) — success flag మరియు result ను exceptions లేకుండా సురక్షితంగా returning, ఒక సాధారణ, idiomatic C# pattern), ref caller's variable ను modify చేయడానికి enable చేస్తుంది, మరియు in large structs ను performance కోసం reference ద్వారా pass చేస్తుంది.
ఈ వాటిని — methods, parameter features (optional/named/params), flexible APIs కోసం overloading, మరియు ref/out/in modifiers (ముఖ్యంగా out-based TryParse idiom) — అర్థం చేసుకోవడం C# methods writing మరియు .NET APIs ఉపయోగించడం రెండింటికీ fundamental, effective C# development కోసం must-know knowledge.