No menu items!
4.3 C
Washington
No menu items!

Using Automatic Field Features: A Beginners Easy Guide.

Date:

Share:

Today I messed around with something called “automatic field” in C#, and I wanna share what I did and how it went. It’s pretty basic stuff, but hey, we all start somewhere, right?

Using Automatic Field Features: A Beginners Easy Guide.

Getting Started

So, I was looking at this code example online and decided to try it out myself. I fired up Visual Studio and created a new console application. You know, the usual – File > New > Project, choose Console App, give it a name, and hit Create.

The Experiment

First, I made a simple class, I’m calling it “MyClass” for now. Just a basic class, nothing fancy:


public class MyClass

Then I added a private string member, and I want that C# can automatic create get and set properties, just like this:


public class MyClass

Using Automatic Field Features: A Beginners Easy Guide.

private string myValue;

Then I want use myValue, I need add get and set method, like this:


public class MyClass

private string myValue;

public string MyValue

Using Automatic Field Features: A Beginners Easy Guide.

get { return myValue; }

set { myValue = value; }

I have set myValue as private,so I can not use myValue directly.

The Result

It worked! I mean, of course, it did, it’s pretty simple. I can set the value and get it back no problem. I added a little bit of code in the Main method to test it out:


static void Main(string[] args)

Using Automatic Field Features: A Beginners Easy Guide.

MyClass instance = new MyClass();

* = "Hello, Automatic Field!";

This is how you use an automatic field, I think this is the best C# * makes the code super clean and easy to read. And it’s less typing, which is always a win in my book.

Subscribe to our magazine

━ more like this

What fits inside the Marc Jacobs tote bag medium size? See if its practical for your daily needs.

Okay, let’s talk about this Marc Jacobs tote bag. The medium one specifically. I kept seeing it around, you know? On social media, people...

Learn more about the actor portraying John B Will (Discover the star behind the OBX role)

Alright, let’s talk about this ‘John B’ thing. Heard the name floating around quite a bit, especially with that show everyone seems to be...

Valentino Born in Roma Donna Green Stravaganza: Is It Worth It?

Okay, so I finally got my hands on the Valentino Born in Roma Donna Green Stravaganza Eau de Parfum. Been seeing it all over...

How to Style a Tory Burch Black Bag with Gold Chain

Alright, buckle up buttercups, ’cause I’m about to spill the tea (and maybe some gold chain links) on my recent adventure with a Tory...

The Best LV Limited Edition Neverfull Styles Youll Love!

Okay, so here’s the lowdown on how I scored my LV Limited Edition Neverfull. It’s a bit of a story, so buckle up! It...

LEAVE A REPLY

Please enter your comment!
Please enter your name here