Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# object initialization can be simplified

//1st
//Before:
TreeNode node = new TreeNode(drive.Substring(0, 1), driveImage, driveImage);
node.Tag = drive;

//After:
var node = new TreeNode(drive.Substring(0, 1), driveImage, driveImage) {
    Tag = drive
};

//2nd
//Before:
DirectoryInfo di = new DirectoryInfo(dir);
TreeNode node = new TreeNode(di.Name, 0, 1); //this line

//After:
var node = new TreeNode((new DirectoryInfo(dir)).Name, 0, 1);

//3rd
//Before:
OleDbCommand select = new OleDbCommand();//this line
select.Connection = cnDTC;
select.CommandText = string.Format("SELECT MAX(VERSION_NO) AS MAX_VERSION FROM ({0})",
      strSQL2);

//After:
var select = new OleDbCommand(
      String.Format("SELECT MAX(VERSION_NO) AS MAX_VERSION FROM ({0})", strSQL2), 
      cnDTC);

//3rd (with string interpolation):
var select = new OleDbCommand($"SELECT MAX(VERSION_NO) AS MAX_VERSION FROM ({strSQL2})", 
      cnDTC);
Comment

PREVIOUS NEXT
Code Example
Csharp :: unity agent does not move 
Csharp :: equivalent to T extends TT in c# 
Csharp :: clear array c# 
Csharp :: change picturebox image c# 
Csharp :: detect keypress c# 
Csharp :: unity 2d looka tt mouse 
Csharp :: Create gaps / headers between variables in the unity inspector 
Csharp :: c# writteline 
Csharp :: unity get list of children 
Csharp :: data table rename column c# 
Csharp :: change array size in unity 
Csharp :: transform.rotate unity 
Csharp :: message uwp c# 
Csharp :: traversing an enum c# 
Csharp :: How to get the world position of the edge of an object? 
Csharp :: how to write a variable in console c# 
Csharp :: Editor log location unity 
Csharp :: prettier inst working c# 
Csharp :: c# unity follow object 
Csharp :: repeat 10 timesw c# 
Csharp :: reverse for loop 
Csharp :: words counter c# 
Csharp :: unity button press onclick click add C# 
Csharp :: if char is upper csharp 
Csharp :: using serial port in c# 
Csharp :: function in Razor Pages 
Csharp :: c# compile code at runtime 
Csharp :: c# to binary 
Csharp :: c# remove special characters from string 
Csharp :: how to create a file through c# script 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =