What does the yield return statement specify in the following C# code snippet?
Posted: Wed Jul 13, 2022 7:57 pm
advertisement
var adpushup = window.adpushup || {};
adpushup.que = adpushup.que || [];
adpushup.que.push(function () {
if (adpushup.config.platform === "MOBILE") {
adpushup.triggerAd("90f55663-effd-4105-b1e7-29d86b526544");
} else if ((window.outerWidth <= 768) || (window.outerWidth == 0)) {
adpushup.triggerAd("90f55663-effd-4105-b1e7-29d86b526544");
}
});
public System.Collections.IEnumerator GetEnumerator() { foreach (char ch in chrs) yield return ch; }a) returns the outputb) returns the next object in the collectionc) Both returns the output & returns the next object in the collectiond) none of the mentioned[expand title="View Answer"] Answer: bExplanation: The yield return statement returns the next object in the collection, which in this case is the next character in chrs in the code.[/expand] 5. What does the following C# code snippet specify?<pre lang="csharp" line="1" cssfile="hk1_style">class MyClass{ char chrs = 'A' ; public IEnumerator GetEnumerator() { for (int i = 20; i >=0; --i) yield return (char)((chrs + i)); }}class Program{ static void Main(string[] args) { MyClass mc = new MyClass(); foreach (char ch in mc) Console.Write(ch + " "); Console.WriteLine(); Console.ReadLine(); }}
a) A B C D E F G H I J K L M N O P Q R S T U V
b) Run time error
c) U T S R Q P O N M L K J I H G F E D C B A
d) Compile successfully prints nothing
var adpushup = window.adpushup || {};
adpushup.que = adpushup.que || [];
adpushup.que.push(function () {
if (adpushup.config.platform === "MOBILE") {
adpushup.triggerAd("90f55663-effd-4105-b1e7-29d86b526544");
} else if ((window.outerWidth <= 768) || (window.outerWidth == 0)) {
adpushup.triggerAd("90f55663-effd-4105-b1e7-29d86b526544");
}
});
public System.Collections.IEnumerator GetEnumerator() { foreach (char ch in chrs) yield return ch; }a) returns the outputb) returns the next object in the collectionc) Both returns the output & returns the next object in the collectiond) none of the mentioned[expand title="View Answer"] Answer: bExplanation: The yield return statement returns the next object in the collection, which in this case is the next character in chrs in the code.[/expand] 5. What does the following C# code snippet specify?<pre lang="csharp" line="1" cssfile="hk1_style">class MyClass{ char chrs = 'A' ; public IEnumerator GetEnumerator() { for (int i = 20; i >=0; --i) yield return (char)((chrs + i)); }}class Program{ static void Main(string[] args) { MyClass mc = new MyClass(); foreach (char ch in mc) Console.Write(ch + " "); Console.WriteLine(); Console.ReadLine(); }}
a) A B C D E F G H I J K L M N O P Q R S T U V
b) Run time error
c) U T S R Q P O N M L K J I H G F E D C B A
d) Compile successfully prints nothing