How amazing C#/.Net is ? Part 1

Photo by Marcus Urbenz on Unsplash

1. Keyword “var” Anonymous Object


Stream stream = new FileStream(“C:\\abc.txt”, FileMode.CreateNew);
var stream = new FileStream(“C:\\abc.txt”, FileMode.CreateNew);
var sampleObj = new { FirstProp = "A", SecondProp = "B"};

2. Automatically create Properties

public class Student
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
public string Name { get; set; }

public string Name { get; private set; } //Field is read only, private setter

3. using blocks

try
{
stream = new FileStream("C:\\abc.txt", FileMode.CreateNew);
stream2 = new FileStream("C:\\abc2.txt", FileMode.CreateNew);
//Do some readding
}
finally
{
if (stream != null) stream.Close();
if (stream2 != null) stream2.Close();
}
using(var stream = new FileStream("C:\\abc.txt", FileMode.CreateNew))
using(var stream2 = new FileStream("C:\\abc.txt", FileMode.CreateNew))
{
//Do some readding
}

4. Measure time with class Stopwatch

var sw = Stopwatch.StartNew();
DoSomething();
sw.Stop();
Console.WriteLine("Time elapsed: " + sw.ElapsedMilliseconds);

5. Ternary operators, null operator

if (input == null)
{
input = "default";
}

input = input != null ? input : "default";
input = input ?? "default"; // If index is null, set by default

6. Initialize objects and collections

Student student = new Student();
student.Name = "John";
student.Age = 10;
Student student = new Student { Name = "John", Age = 10};
// Old way
List <string> list = new List <string> ();
list.Add ("string 1");
list.Add ("string 2");
list.Add ("string 3");

// New way
List <string> list = new List <string> {"string 1", "string 2", "string 3"};
List<Student> list = new List<Student>
{
new Student { Name = "Student 2", Age = 2},
new Student { Name = "Student 3", Age = 4}
};

7. Extension method

public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
public static class StudentExtension
{
public static void Print (this Student student)
{
Console.WriteLine (student.ToString ());
}
}

//Use
var student = new Student ();
student.Print ();
var studentsUnder19 = new List<Student>();
foreach (var student in students)
{
if (student.Age < 19) studentsUnder19.Add(student);
}
var studentsUnder20 = students.Where(student => student.Age < 20);

--

--

--

Always be nice to anybody who has access to my toothbrush.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

FUN IN LINUX😍

A New VIRAL Event Breaks Out In Eggies World! Starting Friday, February 12, 2021.

XXE to SSRF to Windows Administrator Hashes

Work to be surrounded by those you wish to be like

Graph Depth First Search

How I found SQL injection in One of the Indian govt Website.

ResLife Portal — Beta Edition

So you want to write cross platform code, PAL? (Part I)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Beribey

Beribey

Always be nice to anybody who has access to my toothbrush.

More from Medium

Model View Controller Frameworks Explained

CS371p Spring 2022: Sarunas Budreckis — Entry #10

NULL problem in MySQL

Compodoc Integration in Angular App