Scope Method in C#

Now we are going to learn about the scope method in c#.
  • 8091

Introduction

Whenever we declare a variable inside the method that is called method scope. It may also called local scope. Declared variable is valid only particular method. Here we have an x variable defined outside the Show() method. The variable has a class scope. It is valid everywhere inside the definition of the Test class. In other words, between its curly brackets.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

 namespace ConsoleApplication6
{
    public class Test
    {
        int x = 1;
        public void Show()
        {
          Console.WriteLine(this.x);
            Console.WriteLine(x);
        }
    }
    public class CSharpApp
    {
        static void Main()
        {
            Test ts = new Test();
           ts.Show();
            Console.ReadLine();
        }
    }
}

Output

scop.jpg

Note: This article is ref. from   http://zetcode.com/lang/csharp/methods

© 2020 DotNetHeaven. All rights reserved.