Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 347 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C# - An introduction to time saving shortcuts

#1
It seems like many members here are getting into C#, and since I use it on a daily basis for work, I figured I'd give you guys examples of some of the cool features you get from C# that you don't get from other languages.

1. Coalesce operator

How many times have you had some nested class or struct, where some of the properties were nullable? Take this one for example:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


Now, in your program, you would prefer to get .a from test, but if that doesn't exist, you'd like to get it from alternate. You're probably thinking of writing this:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


this works, but it gets exponentially more complicated as you get deeper in levels, I'm sure you can see that. With the coalesce operator though, you don't have to write all of these checks. The following code works the same:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


That double question mark is the coalesce operator. What it does is, if the object on the left of it is null, then it will use the object on the right instead.


2. Null-conditional operator

Let's go back to the above example, but this time we'll add some more levels:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


and we'll initialize it like so

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


and now, let's say our code needs to follow this order in getting the .b double value:
  1. Primary value from primary lvl1
  2. Primary value from alternate lvl1
  3. Secondary value from primary lvl1
  4. Secondary value from alternate lvl1
Sure again here we can use if statements, and that might look something like this:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

but not only is that ugly, it's just a pain in the ass to write...
Now it's time to introduce the null-conditional operator. Whenever you're accessing a member on an object, if you place a question mark before the dot, then C# will stop evaluating at that point if the object is null. What this means is we don't have to do a bunch of error handling, and can just get on with our lives. We can then combine the coalesce operator in such a way that we can reduce this down to the bare minimum number of lines.

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

I'm deliberately not going to break that down and explain each component of it, but I will explain the very first bit:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

this evaluates to something like:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

In conclusion, by making use of the null-conditional operator and coalesce operators, we've managed to turn 10 lines of if-elseif-else into 1 single line.

3. inline declarations
Any serious C# programmer isn't running Convert.ToInt32 on user-entered input (or if they are, there's a pile of exception handlers involved). No, we're using int.TryParse. Unfortunately though, that method doesn't return our number, it returns bool. So we have to pass in our desired result as an out parameter. This means we need to waste a line declaring it, right? Well, no. This one is going to be the shortest in this list, but you can actually declare those right in your function call. The following is totally valid code:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


4. returning multiple parameters
Have you ever had a lambda function that you're writing and you need to return more than one parameter? It's a lambda, so out parameters are out of the question, you wind up wrapping things into a new class that's just used for that one lambda. Turns out you don't need to do any of that. C# can create a tuple for you on the backend (with as many values as you want), and then deconstruct that into the callee. The best context for this is doing some background work and then returning to the UI thread to update the screen. Let's look at an example:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

but what if, instead of doing something like that, we want to generate 2 random numbers, sleep the background thread a random number of seconds between the two numbers, and then return those 3 numbers back so they can be shown on screen? Well, thankfully we actually can do this.

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

You'll see that simply wrapping our parameters in parenthesis will allow C# to appear as if you're returning multiple values. What you're really getting is Tuple<int,int,int>, but at least this way, you don't have to worry about managing placement, names, etc.


5. String interpolation
And to round this tutorial out, you may have noticed the writeline in the above final example. What does that dollar sign mean? Well, what's written there:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

is equivalent to

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

placing a dollar sign in front of your string tells C# to treat it as if it were String.Format, and replace whatever is within the brackets with it's evaluation.

Well, hope you guys enjoyed, if you did, please let me know.
Reply

#2
So simple, yet so effective. I'm by no means a hardcore coder, but there's certainly many of your tips which can be applied to all areas in programming which is incredibly good!

Nice to see you active again
Reply

#3
So simple, yet so effective. I'm by no means a hardcore coder, but there's certainly many of your tips which can be applied to all areas in programming which is incredibly good!

Nice to see you active again
Reply

#4
Nice tutorial, I've currently ended my last year in college (i'm studying Cyber Security) and the languages I learned were C#, PHP, Powershell and Python.
But the C# they learned me looks way different than yours? Here's an example:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

Reply

#5
@"Malcom" here's an example of a single function taken from an in-production product:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


notice the extreme density in that, every line of code is milked to do as many things as I can get out of it.
Reply

#6
@"Malcom" here's an example of a single function taken from an in-production product:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


notice the extreme density in that, every line of code is milked to do as many things as I can get out of it.
Reply

#7
Nice tutorial, I've currently ended my last year in college (i'm studying Cyber Security) and the languages I learned were C#, PHP, Powershell and Python.
But the C# they learned me looks way different than yours? Here's an example:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

Reply

#8
Quote:(07-28-2019, 10:55 PM)Malcom Wrote:

[To see links please register here]

Nice tutorial, I've currently ended my last year in college (i'm studying Cyber Security) and the languages I learned were C#, PHP, Powershell and Python.
But the C# they learned me looks way different than yours? Here's an example:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

Not to be overly harsh, but if you studied cyber security and you didn't cover C, then you got robbed. On top of that, universities today don't teach their students to innovate, they just teach them to pass the tests and maintain the accreditation of the school so that it can continue making money. You'll get better at C# with time, and what you have looks like perfectly fine C# code, but it's not efficient or easy to read. It's a good starting point, you should focus your efforts on the following:

1. removing unnecessary lines of code
2. getting rid of adding things to strings
3. efficiency in both memory and CPU cycles
4. simplicity
5. error handling. IMO, if you write code that doesn't even need additional error checks (use C#'s built ins to your advantage), you're on the right path.
Reply

#9
Quote:(07-28-2019, 10:55 PM)Malcom Wrote:

[To see links please register here]

Nice tutorial, I've currently ended my last year in college (i'm studying Cyber Security) and the languages I learned were C#, PHP, Powershell and Python.
But the C# they learned me looks way different than yours? Here's an example:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

Not to be overly harsh, but if you studied cyber security and you didn't cover C, then you got robbed. On top of that, universities today don't teach their students to innovate, they just teach them to pass the tests and maintain the accreditation of the school so that it can continue making money. You'll get better at C# with time, and what you have looks like perfectly fine C# code, but it's not efficient or easy to read. It's a good starting point, you should focus your efforts on the following:

1. removing unnecessary lines of code
2. getting rid of adding things to strings
3. efficiency in both memory and CPU cycles
4. simplicity
5. error handling. IMO, if you write code that doesn't even need additional error checks (use C#'s built ins to your advantage), you're on the right path.
Reply

#10
Quote:(07-28-2019, 10:59 PM)phyrrus9 Wrote:

[To see links please register here]

Quote: (07-28-2019, 10:55 PM)Malcom Wrote:

[To see links please register here]

Nice tutorial, I've currently ended my last year in college (i'm studying Cyber Security) and the languages I learned were C#, PHP, Powershell and Python.
But the C# they learned me looks way different than yours? Here's an example:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

Not to be overly harsh, but if you studied cyber security and you didn't cover C, then you got robbed. On top of that, universities today don't teach their students to innovate, they just teach them to pass the tests and maintain the accreditation of the school so that it can continue making money. You'll get better at C# with time, and what you have looks like perfectly fine C# code, but it's not efficient or easy to read. It's a good starting point, you should focus your efforts on the following:

1. removing unnecessary lines of code
2. getting rid of adding things to strings
3. efficiency in both memory and CPU cycles
4. simplicity
5. error handling. IMO, if you write code that doesn't even need additional error checks (use C#'s built ins to your advantage), you're on the right path.
I live in Europe, in the first year we always get kind of the easy stuff, the second and third year will be focussing on other courses. I will put all the courses I will study in my 3 years below. (I left out all the business and economic courses)


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through