It is usually not a good idea to mix signed and unsigned integers in arithmetic operations. For example, what will be output of following example?
#include <stdio.h>
int main(void)
{
unsigned int a = 1000;
signed int b = -1;
if (a > b) puts("a is more than b"...
This example shows how to use the default logging api.
import java.util.logging.Level;
import java.util.logging.Logger;
public class MyClass {
// retrieve the logger for the current class
private static final Logger LOG = Logger.getLogger(MyClass.class.getName());
pub...
One of the more popular .NET providers for Postgresql is Npgsql, which is ADO.NET compatible and is used nearly identically as other .NET database providers.
A typical query is performed by creating a command, binding parameters, and then executing the command. In C#:
var connString = "Host=m...
The following example shows how you can use Json.Net to serialize the data in an C# Object's instance, to JSON string.
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; s...
The following example shows how you can deserialize a JSON string containing into an Object (i.e. into an instance of a class).
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Program
{
public class Employee
{
public s...
Declare public variables and methods type in the interface to define how other typescript code can interact with it.
interface ISampleClassInterface {
sampleVariable: string;
sampleMethod(): void;
optionalVariable?: string;
}
Here we create a class that implements the interface.
...
You can open a connection (i.e. request one from the pool) using a context manager:
with engine.connect() as conn:
result = conn.execute('SELECT price FROM products')
for row in result:
print('Price:', row['price'])
Or without, but it must be closed manually:
conn = engine.co...
Python has only limited support for parsing ISO 8601 timestamps. For strptime you need to know exactly what format it is in. As a complication the stringification of a datetime is an ISO 8601 timestamp, with space as a separator and 6 digit fraction:
str(datetime.datetime(2016, 7, 22, 9, 25, 59, 55...
Given a JList like
JList myList = new JList(items);
the selected items in the list can be modified through the ListSelectionModel of the JList:
ListSelectionModel sm = myList.getSelectionModel();
sm.clearSelection(); // clears the selection
sm.setSelectionInterval(inde...
The Iterator.remove() method is an optional method that removes the element returned by the previous call to Iterator.next(). For example, the following code populates a list of strings and then removes all of the empty strings.
List<String> names = new ArrayList<>();
names.add("...
A class in Scala is a 'blueprint' of a class instance. An instance contains the state and behavior as defined by that class. To declare a class:
class MyClass{} // curly braces are optional here as class body is empty
An instance can be instantiated using new keyword:
var instance = new MyClas...
The only way to catch exception in initializer list:
struct A : public B
{
A() try : B(), foo(1), bar(2)
{
// constructor body
}
catch (...)
{
// exceptions from the initializer list and constructor are caught here
// if no exception is thrown h...
struct A
{
~A() noexcept(false) try
{
// destructor body
}
catch (...)
{
// exceptions of destructor body are caught here
// if no exception is thrown here
// then the caught exception is re-thrown.
}
};
Note that, although this...
It is very convenient to use extension methods with interfaces as implementation can be stored outside of class and all it takes to add some functionality to class is to decorate class with interface.
public interface IInterface
{
string Do()
}
public static class ExtensionMethods{
pu...
we can annotate fields with @BindView and a view ID for Butter Knife to find and automatically cast the corresponding view in our layout.
Binding Views
Binding Views in Activity
class ExampleActivity extends Activity {
@BindView(R.id.title) TextView title;
@BindView(R.id.subtitle) TextView ...
This is an example of how to use the generic type TFood inside Eat method on the class Animal
public interface IFood
{
void EatenBy(Animal animal);
}
public class Grass: IFood
{
public void EatenBy(Animal animal)
{
Console.WriteLine("Grass was eaten by: {0}",...
In the condition of the for and while loops, it's also permitted to declare an object. This object will be considered to be in scope until the end of the loop, and will persist through each iteration of the loop:
for (int i = 0; i < 5; ++i) {
do_something(i);
}
// i is no longer in scope...