My Account   Contact Us    
  
» HomeProductsSupportDeveloper CommunityCompany
C# Properties Example
Author: Pete Freitag
Category: c
Shows how to define a property in C# and make use of accessors

using System;

namespace MyNameSpace

{

      public class Foo

      {

            public Foo()

            {

                  x = 0;

            }

            private int x;

            public int X

            {

                  get

                  {

                        return x;

                  }

                  set

                  {

                        if (value > 0)

                        {

                              x = value;

                        }

                        else { x = 0; }

                  }

            }

            static void Main(string[] args)

            {

                  Foo f = new Foo();

                  f.X = 7;

                  Console.WriteLine("x = {0}", f.X);

                  f.X = -3;

                  Console.WriteLine("x = {0}", f.X);

            }

      }
}

This code will output
x = 7
x = 0



Content Management Powered by ActivEdit  ActivEdit Browser Based WYSIWYG HTML Editor

More Code Samples

Sign up for our newsletter: | Subscribe with RSS: RSS
© ActivSoftware 1999 to 2005 | Privacy Statement