TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# String Property

This C# example program shows a string property. Strings can be get and set.

String properties are useful.

They give you another level of abstraction on what the string means. We can simplify a program by avoiding null strings (or other values) when those values do not make sense—or will complicate the code.

Example. First, this example can help you with the basic syntax of using properties, and also string properties. You can declare a property the same way as a field, but you must specify the get and set accessors with those keywords.

Note: The example inserts logic into the property, so it must use the curly braces on the get accessor.

Class that uses string as property, Employee.cs: C#

public class Employee
{
    string _name;

    public string Name
    {
	get
	{
	    // Return the actual name if it is not null.
	    return this._name ?? string.Empty;
	}
	set
	{
	    // Set the employee name field.
	    this._name = value;
	}
    }
}

Main method, Program.cs: C#

using System;

class Program
{
    static void Main()
    {
	// Hire an employee.
	Employee employee = new Employee();

	// Is the name null?
	Console.WriteLine(employee.Name == null);
	Console.WriteLine(employee.Name.Length);

	// Set the name.
	employee.Name = "Sam";

	// Write the length of employee name.
	Console.WriteLine(employee.Name.Length);
    }
}

Output

False
0
3

We define an Employee class in the first section. The Employee class is a public instance class, which means you can declare it in any accessibility domain. It defines a field and a property, named "_name" and Name.

The Employee property Name in the Employee.cs file accesses the backing store _name field. The field and the property are both of type string. The Employee property defines two accessors, get and set.

Tip: You can sometimes only define one accessor and sometimes use different syntax.

The set and get keywords in the Name property in Employee.cs both have enclosed blocks. Properties in the C# language are considered function types as they contain executable code. You can insert logic into either part of the property.

Note: In the Name get accessor, the "??" operator returns the _name field if it is not null, and string.Empty otherwise.

string.EmptyNull Coalescing Operator

The Main method creates an instance of the Employee class on the managed heap. It accesses the Name property. Because the _name field in Employee is initialized to null, when you access Name here the value returned is string.Empty.

But: If Name was null, accessing its Length member would result a NullReferenceException.

Discussion. I have found it useful to define static string properties as global variables in the C# language with custom accessors. The technique of wrapping global variables in accessor methods is described well in Code Complete.

So: This gives you another level of abstraction over your global variables. See Code Complete, 2nd Edition by Steve McConnell, page 340.

When defining other kinds of properties, such as array properties, Microsoft recommends avoiding returning null and instead returning an empty array. This is similar to the null property shown.

Array Property

Summary. We used strings as properties in the C# programming language. This can help simplify your code and reduce the chance of null references. We inserted logic into the get accessor on a string property.

Also: We saw global variables—and how you can use properties in the recommended way.

Global Variable


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf