StackFrame Constructors

Definition

Initializes a new instance of the StackFrame class.

Overloads

StackFrame()

Initializes a new instance of the StackFrame class.

StackFrame(Int32)

Initializes a new instance of the StackFrame class that corresponds to a frame above the current stack frame.

StackFrame(Boolean)

Initializes a new instance of the StackFrame class, optionally capturing source information.

StackFrame(String, Int32)

Initializes a new instance of the StackFrame class that contains only the given file name and line number.

StackFrame(Int32, Boolean)

Initializes a new instance of the StackFrame class that corresponds to a frame above the current stack frame, optionally capturing source information.

StackFrame(String, Int32, Int32)

Initializes a new instance of the StackFrame class that contains only the given file name, line number, and column number.

StackFrame()

Source:
StackFrame.cs
Source:
StackFrame.cs
Source:
StackFrame.cs

Initializes a new instance of the StackFrame class.

public StackFrame ();

Applies to

.NET 9 og andre versioner
Produkt Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackFrame(Int32)

Source:
StackFrame.cs
Source:
StackFrame.cs
Source:
StackFrame.cs

Initializes a new instance of the StackFrame class that corresponds to a frame above the current stack frame.

public StackFrame (int skipFrames);

Parameters

skipFrames
Int32

The number of frames up the stack to skip.

Examples

The following example demonstrates the use of the StackFrame(Int32) constructor. This code example is part of a larger example provided for the StackFrame class.

public void InternalMethod()
{
   try
   {
      ClassLevel2 nestedClass = new ClassLevel2();
      nestedClass.Level2Method();
   }
   catch (Exception e)
   {
      Console.WriteLine(" InternalMethod exception handler");

      // Build a stack trace from one frame, skipping the
      // current frame and using the next frame.  By
      // default, file and line information are not displayed.
      StackTrace st = new StackTrace(new StackFrame(1));
      Console.WriteLine(" Stack trace for next level frame: {0}",
         st.ToString());
      Console.WriteLine(" Stack frame for next level: ");
      Console.WriteLine("   {0}", st.GetFrame(0).ToString());

      Console.WriteLine(" Line Number: {0}",
         st.GetFrame(0).GetFileLineNumber().ToString());

      Console.WriteLine();
      Console.WriteLine("   ... throwing exception to next level ...");
      Console.WriteLine("-------------------------------------------------\n");
      throw e;
   }
}

Applies to

.NET 9 og andre versioner
Produkt Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackFrame(Boolean)

Source:
StackFrame.cs
Source:
StackFrame.cs
Source:
StackFrame.cs

Initializes a new instance of the StackFrame class, optionally capturing source information.

public StackFrame (bool needFileInfo);
public StackFrame (bool fNeedFileInfo);

Parameters

fNeedFileInfoneedFileInfo
Boolean

true to capture the file name, line number, and column number of the stack frame; otherwise, false.

Examples

The following example demonstrates the use of the StackFrame(Boolean) constructor. This code example is part of a larger example provided for the StackFrame class.

[STAThread]
static void Main()
 {
     ClassLevel1 mainClass = new ClassLevel1();

     try {
         mainClass.InternalMethod();
     }
     catch (Exception) {
        Console.WriteLine(" Main method exception handler");

        // Display file and line information, if available.
        StackTrace st = new StackTrace(new StackFrame(true));
        Console.WriteLine(" Stack trace for current level: {0}",
            st.ToString());
        Console.WriteLine(" File: {0}",
           st.GetFrame(0).GetFileName());
        Console.WriteLine(" Line Number: {0}",
            st.GetFrame(0).GetFileLineNumber().ToString());

        Console.WriteLine();
        Console.WriteLine("-------------------------------------------------\n");
     }
 }

Applies to

.NET 9 og andre versioner
Produkt Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackFrame(String, Int32)

Source:
StackFrame.cs
Source:
StackFrame.cs
Source:
StackFrame.cs

Initializes a new instance of the StackFrame class that contains only the given file name and line number.

public StackFrame (string? fileName, int lineNumber);
public StackFrame (string fileName, int lineNumber);

Parameters

fileName
String

The file name.

lineNumber
Int32

The line number in the specified file.

Examples

The following example demonstrates the use of the StackFrame(String, Int32) constructor. This code example is part of a larger example provided for the StackFrame class.

public void Level3Method()
{
   try
   {
      ClassLevel4 nestedClass = new ClassLevel4();
      nestedClass.Level4Method();
   }
   catch (Exception e)
   {
      Console.WriteLine(" Level3Method exception handler");

      // Build a stack trace from a dummy stack frame.
      // Explicitly specify the source file name and
      // line number.
      StackTrace st = new StackTrace(new StackFrame("source.cs", 60));
      Console.WriteLine(" Stack trace with dummy stack frame: {0}",
                  st.ToString());
      for(int i =0; i< st.FrameCount; i++ )
      {
         // Display the stack frame properties.
         StackFrame sf = st.GetFrame(i);
         Console.WriteLine(" File: {0}", sf.GetFileName());
         Console.WriteLine(" Line Number: {0}",
            sf.GetFileLineNumber());
         // Note that the column number defaults to zero
         // when not initialized.
         Console.WriteLine(" Column Number: {0}",
            sf.GetFileColumnNumber());
         if (sf.GetILOffset() != StackFrame.OFFSET_UNKNOWN)
         {
            Console.WriteLine(" Intermediate Language Offset: {0}",
               sf.GetILOffset());
         }
         if (sf.GetNativeOffset() != StackFrame.OFFSET_UNKNOWN)
         {
            Console.WriteLine(" Native Offset: {0}",
               sf.GetNativeOffset());
         }
      }
      Console.WriteLine();
      Console.WriteLine("   ... throwing exception to next level ...");
      Console.WriteLine("-------------------------------------------------\n");
      throw e;
   }
}

Remarks

Use this constructor when you do not want to use the debugger's line mapping logic.

Applies to

.NET 9 og andre versioner
Produkt Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackFrame(Int32, Boolean)

Source:
StackFrame.cs
Source:
StackFrame.cs
Source:
StackFrame.cs

Initializes a new instance of the StackFrame class that corresponds to a frame above the current stack frame, optionally capturing source information.

public StackFrame (int skipFrames, bool needFileInfo);
public StackFrame (int skipFrames, bool fNeedFileInfo);

Parameters

skipFrames
Int32

The number of frames up the stack to skip.

fNeedFileInfoneedFileInfo
Boolean

true to capture the file name, line number, and column number of the stack frame; otherwise, false.

Examples

The following example demonstrates the use of the StackFrame(Int32, Boolean) constructor. This code example is part of a larger example provided for the StackFrame class.

public void Level2Method()
{
   try
   {
      ClassLevel3 nestedClass = new ClassLevel3();
      nestedClass.Level3Method();
   }
   catch (Exception e)
   {
      Console.WriteLine(" Level2Method exception handler");

      // Display the full call stack at this level.
      StackTrace st1 = new StackTrace(true);
      Console.WriteLine(" Stack trace for this level: {0}",
         st1.ToString());

      // Build a stack trace from one frame, skipping the current
      // frame and using the next frame.
      StackTrace st2 = new StackTrace(new StackFrame(1, true));
      Console.WriteLine(" Stack trace built with next level frame: {0}",
         st2.ToString());

      // Build a stack trace skipping the current frame, and
      // including all the other frames.
      StackTrace st3 = new StackTrace(1, true);
      Console.WriteLine(" Stack trace built from the next level up: {0}",
         st3.ToString());

      Console.WriteLine();
      Console.WriteLine("   ... throwing exception to next level ...");
      Console.WriteLine("-------------------------------------------------\n");
      throw e;
   }
}

Applies to

.NET 9 og andre versioner
Produkt Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackFrame(String, Int32, Int32)

Source:
StackFrame.cs
Source:
StackFrame.cs
Source:
StackFrame.cs

Initializes a new instance of the StackFrame class that contains only the given file name, line number, and column number.

public StackFrame (string? fileName, int lineNumber, int colNumber);
public StackFrame (string fileName, int lineNumber, int colNumber);

Parameters

fileName
String

The file name.

lineNumber
Int32

The line number in the specified file.

colNumber
Int32

The column number in the specified file.

Examples

The following example demonstrates the use of the StackFrame constructor. This code example is part of a larger example provided for the StackFrame class.

try
{
   ClassLevel5 nestedClass = new ClassLevel5();
   nestedClass.Level5Method();
}
catch (Exception e)
{
   Console.WriteLine(" Level4Method exception handler");

   // Build a stack trace from a dummy stack frame.
   // Explicitly specify the source file name, line number
   // and column number.
   StackTrace st = new StackTrace(new StackFrame("source.cs", 79, 24));
   Console.WriteLine(" Stack trace with dummy stack frame: {0}",
                  st.ToString());

   // Access the StackFrames explicitly to display the file
   // name, line number and column number properties.
   // StackTrace.ToString only includes the method name.
   for(int i =0; i< st.FrameCount; i++ )
   {
      StackFrame sf = st.GetFrame(i);
      Console.WriteLine(" File: {0}", sf.GetFileName());
      Console.WriteLine(" Line Number: {0}",
         sf.GetFileLineNumber());
      Console.WriteLine(" Column Number: {0}",
         sf.GetFileColumnNumber());
   }
   Console.WriteLine();
   Console.WriteLine("   ... throwing exception to next level ...");
   Console.WriteLine("-------------------------------------------------\n");
   throw e;
}

Remarks

Use this constructor when you do not want to use the debugger's line mapping logic.

Applies to

.NET 9 og andre versioner
Produkt Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1