No menu items!
10.8 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

Swarovski Boston Prudential: Find Your Sparkle Here!

My Trip to Swarovski in Boston’s Prudential Center So, I was at the Prudential Center the other day. Wasn’t really there for anything specific, you...

Clermont Twins before and after: From then to now, a complete visual journey you absolutely must see.

So, you’ve probably seen all the buzz about the Clermont Twins, right? Specifically, those “before and after” photos that are plastered all over the...

Shopping at the Hermes store Boston? Smart tips to help you get what you really want.

So, I found myself in Boston not too long ago. Had a bit of free time on my hands, you know how it is....

Gucci Bear by Harry Styles: Where to Buy Yours

Alright, so I’ve been meaning to share this little project I got myself into. You know how much I adore Harry Styles, and his...

Curious about Viktor and Rolf FKA Twigs work? Everything you absolutely need to know is right here!

So, I was just kind of browsing the other day, you know how it is, just clicking around, and this Viktor & Rolf thing...

LEAVE A REPLY

Please enter your comment!
Please enter your name here