- A method which s defined using the static modifier is a static method is a static method otherwise all the other methods are non static only.
- While defining static methods under a class we need to make sure that we are not consuming any non static members of class under a static method because direct consumption of non static is not possible under static methods, to consume hat first we need to create the object of class and with that object only non static members can be consumed under static block.
RULE : Static members of a class can be consumed under non static blocks of the class directly but non static members must be consumed in a static block only with the help of class object.
using System; namespace CSharpConsole { class StatMets { int x = 100; static int y = 200; static void Add() { StatMets obj = new StatMets(); Console.WriteLine(obj.x + y); } static void Main() { StatMets.Add(); Console.ReadLine(); } } }
No comments:
Post a Comment