BlockStatement blockStatement = new BlockStatement(); blockStatement.Statements.Add(new CodeVariableDeclarationStatement("int", "num", new CodePrimitiveExpression(10))); blockStatement.Statements.Add(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("Console"), "WriteLine", new CodeVariableReferenceExpression("num")));
BlockStatement blockStatement = new BlockStatement(); CodeExpression left = new CodePrimitiveExpression(10); CodeExpression right = new CodePrimitiveExpression(20); CodeExpression condition = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.LessThan, right); CodeStatement trueStatement = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("Console"), "WriteLine", new CodePrimitiveExpression("left is less then right")); CodeStatement falseStatement = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("Console"), "WriteLine", new CodePrimitiveExpression("left is greater than or equal to right")); CodeConditionStatement ifStatement = new CodeConditionStatement(condition, trueStatement, falseStatement); blockStatement.Statements.Add(ifStatement);In this example, a new BlockStatement object is created and an if-else statement is added to it. The condition for the if statement is created using BinaryOperatorExpression and checks if the variable "left" is less than the variable "right". The true statement logs a message stating that "left" is less than "right" to the console. The false statement logs a message stating that "left" is greater than or equal to "right" to the console. The if-else statement is then added to the block statement. Package/library: System.CodeDom.