Yield Keyword C# with IEnumerable

Used in an iterator block to provide a value to the enumerator object or to signal the end of iteration.

expression is evaluated and returned as a value to the enumerator object; expressionhas to be implicitly convertible to the yield type of the iterator.

The yield statement can only appear inside an iterator block, which might be used as a body of a method, operator, or accessor. The body of such methods, operators, or accessors is controlled by the following restrictions:

  • Unsafe blocks are not allowed.
  • Parameters to the method, operator, or accessor cannot be ref or out.

yield statement cannot appear in an anonymous method. For more information, seeAnonymous Methods (C# Programming Guide).

When used with expression, a yield return statement cannot appear in a catch block or in a try block that has one or more catch clauses. For more information, seeException Handling Statements (C# Reference).

using System;
using System.Collections.Generic;
 
public class MyClass
{
    public static  IEnumerable<int> Powers(int from,int to)
    {
        for (int i = from; i <= to; ++i)
        {
            yield return (int)Math.Pow(2, i);
        }
    }
 
    static void Main()
    {
        IEnumerable<int> powers = Powers(0, 16);
        foreach (int result in powers)
        {
            Console.WriteLine(result);
        }
    }
}
Source :MSDN

Visual Studio IDE free copy using ALT

I just wanted to copy that code without the comments. So, the trick is to simply press the Alt button, and then highlight the rectangle you like.(e. g. below).

protected void MyCommand(string e)
    {
        //if (e == "Hello")
        //{
        //    lblFog.Text = e.CommandArgument.ToString();
        //}
    }

In the above code if I want to select :

if (e == "Hello")
{
    lblFog.Text = e.CommandArgument.ToString();
}

Then I press ALt key and select the rectangle and no need to uncomment the lines.

Define data type for enums

The data type can be defined for an enumeration:

enum EnumName : [byte, char, int16, int32, int64, uint16, uint32, uint64]
{
    A = 1,
    B = 2
}

Enum Inheritance

this is not possible. Enums cannot inherit from other enums. In fact all enums must actually inherit from System.Enum. C# allows syntax to change the underlying representation of the enum values which looks like inheritance, but in actuality they still inherit from System.enum.

See section 8.5.2 of the CLI spec for the full details. Relevant information from the spec

  • All enums must derive from System.Enum
  • Because of the above, all enums are value types and hence sealed

Source:
http://stackoverflow.com/questions/757684/enum-inheritance/757762#757762

Object size in memory c#

Unfortunately there is no direct way to do it. Speacialy for the managed code. May you can use memorystream and serialization for getting an idea but this are not actual size of your object. You may use some solution below for calculation actual size.

GC
One way is to use the GC.GetTotalMemory method to measure the amount of memory used before and after creating your object. This won’t be perfect, but as long as you control the rest of the application you may get the information you are interested in.
Source :http://stackoverflow.com/questions/605621/how-to-get-object-size-in-memory

Using Strike or SOS Debugging Extension (SOS.dll)
use : DumpHeap [-stat] [-min <size>][-max <size>] [-thinlock] [-mt <MethodTable address>] [-type <partial type name>][start [end]]

Displays information about the garbage-collected heap and collection statistics about objects.
The DumpHeap command displays a warning if it detects excessive fragmentation in the garbage collector heap.
The -stat option restricts the output to the statistical type summary.
The -min option ignores objects that are less than the size parameter, specified in bytes.
The -max option ignores objects that are larger than the size parameter, specified in bytes.
The -thinlock option reports ThinLocks. For more information, see the SyncBlk command.
The -mt option lists only those objects that correspond to specified the MethodTable structure.
The -type option lists only those objects whose type name is a substring match of the specified string.
The start parameter begins listing from the specified address.
The end parameter stops listing at the specified address.

!dumpheap -stat
Note that !dumpheap only gives you the bytes of the object type itself, and doesn’t include the bytes of any other object types that it might reference

More Info:
http://msdn.microsoft.com/en-us/library/bb190764.aspx

3rd party solutions

Using .Net Memory Profiler (Easy way)
http://memprofiler.com/download.aspx

using CLR Profiler (free)
http://www.microsoft.com/downloads/details.aspx?familyid=A362781C-3870-43BE-8926-862B40AA0CD0&displaylang=en

ANTS Memory Profiler
http://www.red-gate.com/products/ants_memory_profiler/index.htm

Note:
Some people have confused the System.Runtime.InteropServices.Marshal.SizeOf() service with this API.  However, Marshal.SizeOf reveals the size of an object after it has been marshaled.  In other words, it yields the size of the object when converted to an unmanaged representation.  These sizes will certainly differ if the CLR’s loader has re-ordered small fields so they can be packed together on a tdAutoLayout type.
source : http://blogs.msdn.com/cbrumme/archive/2003/04/15/51326.aspx

code bye…