This question already has answers here:
When to use in vs ref vs out
(17 answers)
Closed 9 years ago.
When should we actually use ref and out.
I know the difference between the two.
Before you can pass a ref parameter, you must assign it to a value. which is not compulsory in out.
But when should we use ref. ??
I read this, but didnt got, when should I use ref.
http://www.dotnetperls.com/ref
Here is an example:
static void Main(string[] args)
{
int i = 1;
foo(i);
Console.Write(i); //i=1;
Reffoo(ref i);
Console.Write(i); //i=2;
}
static void Reffoo(ref int i)
{
i++;
}
static void foo(int i)
{
i++;
}
Related
This question already has answers here:
Can a Delegate have an optional parameter?
(2 answers)
Closed 9 years ago.
Why this piece of code does not compile?
delegate int xxx(bool x = true);
xxx test = f;
int f()
{
return 4;
}
Optional parameters are for use on the calling side - not on what is effectively like a single-method-interface implementation. So for example, this should compile:
delegate void SimpleDelegate(bool x = true);
static void Main()
{
SimpleDelegate x = Foo;
x(); // Will print "True"
}
static void Foo(bool y)
{
Console.WriteLine(y);
}
What will happen test(false)? It will corrupt the stack, because signatures must match.
Try this way:
static int f(bool a)
{
return 4;
}
This question already has answers here:
Cleanest way to write retry logic?
(30 answers)
Closed 2 years ago.
I wish to create an extension method that takes A function with a parameter of IEnumerable<T>.
int NumberOfRetries = 3;
string TableName = "Table";
Method(EnumerableParameter).RetrySection(EnumerableParameter,TableName ,NumberOfRetries);
Function to be extended
bool Method(IEnumerable<int> param)
{
foreach(var item in param)
{
Console.WriteLine(item);
}
return true;
}
I am having difficulty making the code compile
RetrySection(() => Method(EnumerableParameter),EnumerableParameter,TableName ,NumberOfRetries);
for both options.
Method(EnumerableParameter).RetrySection(EnumerableParameter,TableName,NumberOfRetries);
Extension Method
public static void RetrySection(this Func<IEnumerable<T>,bool> action, IEnumerable<T> parameter,string databaseTable,int jobcount)
{
int jobRowCount = ValidateTable(databaseTable).GetAwaiter().GetResult();
int retry = 0;
do
{
action(parameter);
jobRowCount = ValidateTable(databaseTable).GetAwaiter().GetResult();
retry++;
}
while ((jobRowCount < (jobcount * 2)) && retry < 3);
}
I'm getting the error Func<IEnumerable<int>,bool> does not take 0 arguments.
The solution is to use:
Method.RetrySection(EnumerableParameter,TableName ,NumberOfRetries);
because that is what RetrySection is defined to work on - Func<IEnumerable,bool>, instead of the result - bool.
Or, in case of the second example:
RetrySection(Method,EnumerableParameter,TableName ,NumberOfRetries);
This question already has answers here:
No overflow exception for int in C#?
(6 answers)
Closed 4 years ago.
using System;
namespace test_warmup
{
class Program
{
static void Main(string[] args)
{
int test = -1110835200;
test = test * 1700397056;
Console.WriteLine(test);
}
}
}
-1110835200 * 1700397056 = -1888860903781171200
Displayed in hex, that's -0x1a3694fc_00000000
In C#, int is only 32-bits, so the result is truncated to just 0.
In other words, the result of that multiplication is too large to fit in the variable to which you're assigning it, and the part that fits is all zero.
This question already has answers here:
printing all contents of array in C#
(13 answers)
How does the .ToString() method work?
(4 answers)
Closed 6 years ago.
I am using methods in c#. i returned something but compiler is not printing which i am expecting. It is printing (system.string[]). i dont know why Please help on this.
class Program
{
static void Main(string[] args)
{
string[] sub = subjects();
Console.WriteLine(sub);
}
public static string[] subjects()
{
Console.WriteLine("Please Enter How many Subject Do you Want to input");
int limit = System.Convert.ToInt32(Console.ReadLine());
string[] Subjects = new string[limit];
for (int i = 0; i < limit; i++)
{
Console.WriteLine("Please Enter Subject Name " + i);
Subjects[i] = Console.ReadLine();
}
return Subjects;
}
}
The reason that Program is printing system.string[] is because class string[] does not override ToString method, so the default one is used and the default ToString returns type name, not the "value" inside type.
You can use for example String.Join method to build one string from array of strings and put given delimiter between each string:
Console.WriteLine(String.Join(", ", sub));
Console.WriteLine(sub)
wont show you anything of value (it prints the sub type), since you are not referencing any value of the sub array, e.g sub[0]. Try this:
foreach (string s in sub)
{
Console.WriteLine(s);
}
Console.WriteLine(String.Join(", ", sub));
This question already has an answer here:
String replace not working [duplicate]
(1 answer)
Closed 7 years ago.
I just started learning C# and I am trying to understand how to use the ref and out .
I tried to build the simplest function I could think of but I'm getting error and I can't understand why.
namespace cscheck
{
class Program
{
static void Main(string[] args)
{
string check = "Check noob wallak";
Console.WriteLine(check);
swap(ref check, "noob", "boon");
Console.WriteLine(check);
}
static void swap(ref string origin ,string x, string y)
{
origin.Replace(x, y);
}
}
}
But the results I get are :
Check noob wallak
Check noob wallak
As I understand x and y are passed by value while check passed by reference, but the replace hasn't ouccred and I can figure why.
Because Replace doesn't mutate, it creates a new string. Try:
origin = origin.Replace(x, y);
Replace() returns a string. You must use it as the result as so :
origin = origin.Replace(x, y);
.Replace returns a new string, it does not modify the original. Strings are immutable.