C# Studygroup 0605

2016-06-07
Note

Recently I’ve joined a book club which I found on the social media (PTT), this book club was hold every Sunday. After I’ve joined the book club, I learned lots of new techniques that I used to ignore or I never knew; therefore, I would like to share what I’ve learned from there. The things that I am going to share are some techniques that I’d never notice, so I won’t explain everything from the beginning. And for those that think these techniques are too easy and was written on the book, please forgive me for my ignorance because I wrote this in order to remind myself of some key points.

Foreword

C# is the first programming language that I learned. Actually, in the first semester of my college I used to have learned C and C++; however, I failed the class due to my lack of concentration in class. In the second semester, I decided to learn programming language seriously. At that time, there is a course call Windows Programming and the course is all about C#. During the class, I designed a tower defense game by using C# only. It was a special memory for me and I think there are lots of things that I can improve in order to make the game better. (If I have the time to redo it again lol)

The main purpose that I joined the book club is to study more about programming. Also, most of the member of the book club are non-student. I can learn some techniques of cooperate development, how to improve efficacy and know what’s going on in industry. The book club was divided into two groups, one group study advanced C# and the other study ASP.NET MVC. These two groups will assign weekly homework and on every Sunday both groups will come up with a person that’s going to share some key points and techniques that is important in the homework. I’ll explain what’s MVC in my next review, when I have a full understand of it.

What I’ve learn from book club

This week’s book club we were talking about Type declaration, basically I have had some understand about basic types, such as int, float, string etc. And most of them are all about the “string”.

Verbatim string literals

Some people might need to store a path of a file in to a string while they are using C#; however, we can’t use ‘\’in a string because it will be seen as an escape character. Escape character are something like “\n” (end line) “\t” (tab). Today we assume that we have a file name “Lion.jpg” in a file name “Willake” in local disk-D, and we want to store its path in to a string. Then, we declare the string like this:

1
string path = "D:\Willake\Lion.jpg";

The compiler will tell you that there is an error, so we have to declare it like this:

1
string path = "D:\\Willake\\Lion.jpg";

Its quite bother to add an extra ‘\’ to every ‘\’, but actually we just need to add a ‘@’ in front of the quotation mark and it will be fine. Though you still have to write it in the other way if you really need to use “\n”.

1
string path = @"D:\Willake\Lion.jpg";

Because all the escape characters will be seen as normal character in this method, if you are going to put some words or SQL or even javascript code in to a string, you might need to use a lot of ‘+’ and ‘&’ . This way may cause low efficacy, but if you use ‘@’ it will be much more simple.

Use ‘+’ to concatenate multiple strings will waste the memory space.

We usually face the problem of concatenating multiple strings while we using C#, and what we do is adding a ‘+’ between strings.

1
2
3
4
string msg = "Hello,",Name = "Peter";
msg += Name;
msg += ". Today is ";
msg += System.DateTime.Today.ToShortDateString();

When we use that method, you may think it will be like this in the memory:

Stored the data in the original location of the string. Actually, it will be like this:

Every time we use ‘+’ to concatenating strings, the memory will assign a new space to store the string. The original string won’t be deleted and will become a trash. It is because CLR wants to save the memory space so it build a hash table system in heap. Relative to string, StringBuilder class is better. It is changeable so it won’t assign any new space. we can use it to concatenate strings.

1
2
3
4
5
6
string name = "Peter";
StringBuilder msg = new StringBuilder("Hello, ");
msg.Append(name);
msg.Append(". Today is ");
msg.Append(DateTime.Now.ToShortDateString());
string finalMsg = msg.ToString();

C# 6.0 Interpolated Strings

We used to use String.Format() function to converts the value of objects to strings and inserts them into another.

1
2
3
4
5
string firstName = "Willake";
string lastName = "Lin";
int salary = 80000;

string msg1 = String.Format("{0} {1} 's salary is {2:C0} per month.", firstName, lastName, salary);

In C# 6.0, Microsoft add a new feature called “Interpolated Strings”. You can add a ‘$’ before the string.

1
2
3
4
5
string firstName = "Willake";
string lastName = "Lin";
int salary = 80000;

string msg1 = $"{firstName} {lastName} 's salary is {salary :C0} per month.";

Both of results will display “Willake Lin ‘s salary is NT$80000 per month.”. If there are lots of variables that need to be insert into a string, using string.format() isnt that convenient because you need to compare against each other and that is a huge task.

Conclusion

This is what I learned from the study group on June 5. Everyone in the group is nice, they won’t alienate people who is just a beginner. I’m really looking forward to next gathering, hoping i can learn more interesting things.


Comments: