0% found this document useful (0 votes)
0 views7 pages

C# Property

The document provides an overview of properties in C#, explaining their role as accessors for private fields in a class. It discusses the importance of using properties to maintain control over data integrity and provides examples of property implementation. The conclusion highlights the use of properties in Windows Forms applications and references additional resources for further reading.

Uploaded by

Agha Printhouse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views7 pages

C# Property

The document provides an overview of properties in C#, explaining their role as accessors for private fields in a class. It discusses the importance of using properties to maintain control over data integrity and provides examples of property implementation. The conclusion highlights the use of properties in Windows Forms applications and references additional resources for further reading.

Uploaded by

Agha Printhouse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Kurdistan Regional Government

Sulaimany Polytechnic University


computer science institute
Department of Database - evening
First Stage

C# -
property
Prepared by
Hassan Jasim Muhammad

:Supervised By
Manal Ali
Year of Education
2022 – 2021

Contents
Introduction.............................................................................................................3
what is a property in c#............................................................................................3
Why We use properties...........................................................................................4
Example....................................................................................................................5
Conclusion................................................................................................................6
Refrences................................................................................................................. 7

2|Page
Introduction

what is a property in c#
A property is a member that provides a flexible mechanism to read,
write, or compute the value of a private field. Properties can be used as
if they are public data members, but they are actually special methods
called accessors.
Another Property in C# is a member of a class that provides a flexible
mechanism for classes to expose private fields. Internally, C# properties
are special methods called accessors. A C# property have two
accessors, get property accessor and set property accessor. A get
accessor returns a property value, and a set accessor assigns a new
value. The value keyword represents the value of a property.

3|Page
Why We use properties
Marking the class field public & exposing is a risky, as you will not have
control what gets assigned & returned.
To understand this clearly with an example lets take a student class
who have ID, pass mark , name.Now in this example some problem
with public field

 ID should not be -ve.

 Name can not be set to null

 Pass mark should be read only.

 If student name is missing No Name should be return.

 To remove this problem We use Get and set method.

4|Page
Example 1

class Person { private string name; // field public string Name //


property { get { return name; } // get method set { name = value; } // set
method } }

Example 2

class Person { private string name; // field public string Name //


property { get { return name; } set { name = value; } } } class Program
{ static void Main(string[] args) { Person myObj = new Person();
myObj.Name = "Liam"; Console.WriteLine(myObj.Name); } }

5|Page
Conclusion

Classes in the System.Windows.Forms namespace are used to create


rich-client applications using C#. The Form class is the base class for
most windows in a Windows Forms application. When you add a new
form to your Visual C# project, the class will be created with a number
of necessary methods already defined. The Form class exposes a
number of properties that can be changed programmatically or through
the Forms Designer’s Properties window.

The MessageBox class is used to display message boxes to users and


includes a wide variety of options that control the appearance of the
message box. Using the MessageBox class, you can control the display
of icons, button placement, and other properties.

A Windows Forms application is controlled by the Application class,


which is responsible for starting the application’s message loop and
displaying the top-level window. The Application class also includes
properties that calculate the proper locations for storing per-user or
application data. Instead of creating an instance of
the Application class, you call static methods exposed by the class, such
as Application.Run and Application.Exit. You can also use form
properties to control form behaviors such as setting a form’s border
style and defining its background color.

6|Page
Refrences

 https://www.w3schools.com/cs/cs_properties.php

 http://etutorials.org/Programming/visual-c-sharp

 http://etutorials.org/Programming/visual-c-sharp/

Part+III+Programming+Windows+Forms/

Chapter+11+An+Introduction+to+Windows+Forms/Conclusion/

7|Page

You might also like