Is there regular expression (regex) support available to C# developers? Is there a way to force garbage collection? Does C# support properties of array types? What connections does Microsoft SQL Server support? What is a satellite assembly? How is method overriding different from overloading? When do you absolutely have to declare a class as abstract (as opposed to free-willed educated choice or decision based on UML diagram)? Why would you use untrusted verification? What is the implicit name of the parameter that gets passed into the class set method? How do I register my code for use by classic COM clients? How do I do implement a trace and assert? How do I create a multi language, multi file assembly? C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write? What is the equivalent to regsvr32 and regsvr32 /u a file in .NET development? Where is the output of TextWriterTraceListener redirected? How do I create a multilanguage, single-file assembly? Why cannot you specify the accessibility modifier for methods inside the interface? Is it possible to restrict the scope of a field/method of a class to the classes in the same namespace? Why do I get a syntax error when trying to declare a variable called checked? Why are there five tracing levels in System.Diagnostics.TraceSwitcher? What is the syntax for calling an overloaded constructor within a constructor (this() and constructorname() does not compile)? Why do I get a "CS5001: does not have an entry point defined" error when compiling? What does the keyword virtual mean in the method definition? What optimizations does the C# compiler perform when you use the /optimize+ compiler option? How can I create a process that is running a supplied native executable (e.g., cmd.exe)? What is the difference between the System.Array.CopyTo() and System.Array.Clone()? How do I declare inout arguments in C#? Is there a way of specifying which block or loop to break out of when working with nested loops? What is the difference between const and static read-only? What does the parameter Initial Catalog define inside Connection String? What is the difference between System.String and System.StringBuilder classes? What is the top .NET class that everything is derived from? Can you allow class to be inherited, but prevent the method from being over-ridden? Can you change the value of a variable while debugging a C# application? Are private class-level variables inherited? Can you inherit multiple interfaces?
Yes. The .NET class libraries provide support for regular expressions. Look at the documentation for the System.Text.RegularExpressions namespace.
Yes. Set all references to null and then call System.GC.Collect(). If you need to have some objects destructed, and System.GC.Collect() doesn't seem to be doing it for you, you can force finalizers to be run by setting all the references to the object to null and then calling System.GC.RunFinalizers().
Yes. Here's a simple example: using System;
class Class1
{
private string[] MyField;
public string[] MyProperty
{
get { return MyField; }
set { MyField = value; }
}
}
class MainClass
{
public static int Main(string[] args)
{
Class1 c = new Class1();
string[] arr = new string[] {"apple", "banana"};
c.MyProperty = arr;
Console.WriteLine(c.MyProperty[0]); // "apple"
return 0;
}
}
Windows Authentication (via Active Directory) and SQL Server authentication (via Microsoft SQL Server username and passwords)
When you write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.
When overriding, you change the method behavior for a derived class. Overloading simply involves having a method with the same name within the class.
When at least one of the methods in the class is abstract. When the class itself is inherited from an abstract class, but not all base abstract methods have been over-ridden.
Web Services might use it, as well as non-Windows applications.
Value, and its datatype depends on whatever variable we are changing.
Use the regasm.exe utility to generate a type library (if needed) and the necessary entries in the Windows Registry to make a class available to classic COM clients. Once a class is registered in the Windows Registry with regasm.exe, a COM client can use the class as though it were a COM class.
Use a conditional attribute on the method, as shown below:
class Debug
{
[conditional("TRACE")]
public void Trace(string s)
{
Console.WriteLine(s);
}
}
class MyClass
{
public static void Main()
{
Debug.Trace("hello");
}
}
In this example, the call to Debug.Trace() is made only if the preprocessor symbol TRACE is defined at the call site. You can define preprocessor symbols on the command line by using the /D switch. The restriction on conditional methods is that they must have void return type.
Unfortunately, this is currently not supported in the IDE. To do this from the command line, you must compile your projects into netmodules (/target:module on the C# compiler), and then use the command line tool al.exe (alink) to link these netmodules together.
Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there is no implementation in
Try using RegAsm.exe. The general syntax would be: RegAsm. A good description of RegAsm and its associated switches is located in the .NET SDK docs. Just search on "Assembly Registration Tool".Explain ACID rule of thumb for transactions.
Transaction must be Atomic (it is one unit of work and does not dependent on previous and following transactions), Consistent (data is either committed or roll back, no in-between case where something has been updated and something hasnot), Isolated (no transaction sees the intermediate results of the current transaction), Durable (the values persist if the data had been committed even if the system crashes right after).
To the Console or a text file depending on the parameter passed to the constructor.
This is currently not supported by Visual Studio .NET.
They all must be public. Therefore, to prevent you from getting the false impression that you have any freedom of choice, you are not allowed to specify any accessibility, it is public by default.
There is no way to restrict to a namespace. Namespaces are never units of protection. But if you're using assemblies, you can use the 'internal' access modifier to restrict access to only within the assembly.
The word checked is a keyword in C#.
The tracing dumps can be quite verbose and for some applications that are constantly running you run the risk of overloading the machine and the hard drive there. Five levels range from None to Verbose, allowing to fine-tune the tracing activities.
The syntax for calling another constructor is as follows:
class B
{
B(int i)
{ }
}
class C : B
{
C() : base(5) // call base constructor B(5)
{ }
C(int i) : this() // call C()
{ }
public static void Main() {}
}
The most common problem is that you used a lowercase 'm' when defining the Main method. The correct way to implement the entry point is as follows:
class test
{
static void Main(string[] args) {}
}
The method can be over-ridden.
The following is a response from a developer on the C# compiler team:
We get rid of unused locals (i.e., locals that are never read, even if assigned).
We get rid of unreachable code.
We get rid of try-catch w/ an empty try.
We get rid of try-finally w/ an empty try (convert to normal code...).
We get rid of try-finally w/ an empty finally (convert to normal code...).
We optimize branches over branches:
gotoif A, lab1
goto lab2:
lab1:
turns into: gotoif !A, lab2
lab1:
We optimize branches to ret, branches to next instruction, and branches to branches.
The following code should run the executable and wait for it to exit before
continuing: using System;
using System.Diagnostics;
public class ProcessTest {
public static void Main(string[] args) {
Process p = Process.Start(args[0]);
p.WaitForExit();
Console.WriteLine(args[0] + " exited.");
}
}
Remember to add a reference to System.Diagnostics.dll when you compile.
The first one performs a deep copy of the array, the second one is shallow.
The equivalent of inout in C# is ref. , as shown in the following
example: public void MyMethod (ref String str1, out String str2)
{
...
}
When calling the method, it would be called like this: String s1;
String s2;
s1 = "Hello";
MyMethod(ref s1, out s2);
Console.WriteLine(s1);
Console.WriteLine(s2);
Notice that you need to specify ref when declaring the function and calling it.
The easiest way is to use goto: using System;
class BreakExample
{
public static void Main(String[] args)
{
for(int i=0; i<3; i++)
{
Console.WriteLine("Pass {0}: ", i);
for( int j=0 ; j<100 ; j++ )
{
if ( j == 10) goto done;
Console.WriteLine("{0} ", j);
}
Console.WriteLine("This will not print");
}
done:
Console.WriteLine("Loops complete.");
}
}
The difference is that static read-only can be modified by the containing class, but const can never be modified and must be initialized to a compile time constant. To expand on the static read-only case a bit, the containing class can only modify it: -- in the variable declaration (through a variable initializer).
-- in the static constructor (instance constructors if it's not static).
The database name to connect to.
System.String is immutable; System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.
System.Object.
Yes, just leave the class public and make the method sealed
Yes, if you are debugging via Visual Studio.NET, just go to Immediate window.
Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But they are.
Yes. .NET does support multiple interfaces.
C# Questions And Answers Continue ...
Labels: C# Technical