Why and where we use param, using and yeild keywords [Resolved]

Posted by Shreedar under C# on 9/28/2015 | Points: 10 | Views : 328 | Status : [Member] | Replies : 3
I faced the below questions in interview which am not aware of.
They asked have you used param using and yeild keywords.
I answered for using as we use for automatic closing of a opened connection
we use the keyword using

Regards

Sridhar Thota.
www.dotnet-sridhar.blogspot.com



Responses

Posted by: Rajnilari2015 on: 9/28/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
2
Down

Resolved
Param : If we want to pass variable number of arguments from calling function to called function, we can use the Params Arrays
e.g.
public class ParamTest

{
static void Main()
{
//calling function
PerformArithmeticOperation(20,10,"+"); //perform addition
PerformArithmeticOperation(20,10,"-"); //perform subtraction
PerformArithmeticOperation(20,10,"*"); //perform multiplication
PerformArithmeticOperation(20,10,"/"); //perform division
}

//called function
public static void PerformArithmeticOperation(params object[] items)
{
var result = 0;
switch (Convert.ToString(items[2])) //items[2] is the operator(s) i.e. +,-,*,/
{
case '+': result = Convert.ToInt32(items[0]) + Convert.ToInt32(items[1]); break;
case '-': result = Convert.ToInt32(items[0]) - Convert.ToInt32(items[1]); break;
case '*': result = Convert.ToInt32(items[0]) * Convert.ToInt32(items[1]); break;
case '/': result = Convert.ToInt32(items[0]) / Convert.ToInt32(items[1]); break;
default: Console.WriteLine("No such operation available");
}
Console.WriteLine(result);
}
}

You also can send no arguments. If you send no arguments, the length of the params list is zero.
Once I wrote an article (http://www.dotnetfunda.com/articles/show/1838/invoke-javascript-methods-from-silverlight-applications) where you can find the use of Params arrays.

Using : In C# parlance, Using can be use in two ways

a) As a directive where it's behaviour will be as inclusion of a namespace. e.g.

using System; //import namespace

using System.IO;
using Item = System.Collections.Generic; //creating alias for namespace
namespace UsingNamespaceDemo
{
public class TestUsingClass
{
//observe Item.List where Item is an alias of System.Collections.Generic namespace
var intItemList = new Item.List<int>(){1,2,3,4,5};
foreach(var item in intItemList) Console.WriteLine(item);
}
}

b)As a statement that can only be used with types that implement IDisposable interface.
e.g.
string connString = "Data Source=abc;Integrated Security=SSPI;Initial Catalog=db;";

using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM tblTest";
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
Console.WriteLine("{0}", dr.GetString(0));
}
}

It can also be consider as a syntactic sugar(short hand version) for try/finally block e.g.
using (SqlConnection conn = new SqlConnection(connString))

{
//do something
}
is same as

SqlConnection conn = new SqlConnection(connString);

try
{
//do something
}
finally
{
if(conn != null)
{
conn.Dispose();
}
}

Yield :
It performs ITERATION .
It was inroduce in C# 2.0. When we use the yield keyword in a statement, we indicate that the method, operator, or get accessor in which it appears is an iterator.It creates a state machine "under the covers" that remembers where you were on each additional cycle of the function and picks up from there.

In short,Yield has two great uses

a) It helps to provide custom iteration with out creating temp collections.

b) It helps to do stateful iteration.


you can watch http://www.dotnetfunda.com/articles/show/3214/performing-crud-operation-with-ravendb-using-rss-feed-as-an-use-case for more clarity.

Hope this helps.

--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shreedar on: 9/28/2015 [Member] Starter | Points: 25

Up
0
Down
Raj simply superb...
you deserves the best answer tag.

Regards

Sridhar Thota.
www.dotnet-sridhar.blogspot.com

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 9/28/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Shreedar,glad to know that it has helped you.

--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response