Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Get/Set Syntax Usage

These are declarations for a Person class.

protected int ID { get; set; } protected string Title { get; set; } protected string Description { get; set; } protected TimeSpan jobLength { get; set; } 

How do I go about using the get/set? In main, I instantiate a

Person Tom = new Person(); 

How does Tom.set/get??

I am use to doing C++ style where you just write out the int getAge() and void setAge() functions. But in C# there are shortcuts handling get and set?

like image 489
RoR Avatar asked Jan 11 '11 20:01

RoR


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


2 Answers

Assuming you have access to them (the properties you've declared are protected), you use them like this:

Person tom = new Person(); tom.Title = "A title"; string hisTitle = tom.Title; 

These are properties. They're basically pairs of getter/setter methods (although you can have just a getter, or just a setter) with appropriate metadata. The example you've given is of automatically implemented properties where the compiler is adding a backing field. You can write the code yourself though. For example, the Title property you've declared is like this:

private string title; // Backing field protected string Title {     get { return title; }  // Getter     set { title = value; } // Setter } 

... except that the backing field is given an "unspeakable name" - one you can't refer to in your C# code. You're forced to go through the property itself.

You can make one part of a property more restricted than another. For example, this is quite common:

private string foo; public string Foo {     get { return foo; }     private set { foo = value; } } 

or as an automatically implemented property:

public string Foo { get; private set; } 

Here the "getter" is public but the "setter" is private.

like image 88
Jon Skeet Avatar answered Sep 23 '22 19:09

Jon Skeet


Assuming you have a song class (you can refer below), the traditional implementation would be like as follows

 class Song   {        private String author_name;        public String setauthorname(String X) {}; //implementation goes here        public String getauthorname() {}; //implementation goes here   } 

Now, consider this class implementation.

      class Song        {             private String author_name;             public String Author_Name             {                   get { return author_name; }                 set { author_name= value; }              }       } 

In your 'Main' class, you will wrote your code as

    class TestSong     {        public static void Main(String[] Args)       {           Song _song = new Song(); //create an object for class 'Song'               _song.Author_Name = 'John Biley';           String author = _song.Author_Name;                      Console.WriteLine("Authorname = {0}"+author);       }     } 

Point to be noted;

The method you set/get should be public or protected(take care) but strictly shouldnt be private.

like image 21
now he who must not be named. Avatar answered Sep 21 '22 19:09

now he who must not be named.